使用beautifulSoup,Python在h3和div标签中刮取文本 - python

我没有使用python,BeautifulSoup,Selenium等的经验,但是我很想从网站上抓取数据并将其存储为csv文件。
我需要的单个数据样本编码如下(一行数据)。

<div class="box effect">
<div class="row">
<div class="col-lg-10">
    <h3>HEADING</h3>
        <div><i class="fa user"></i>&nbsp;&nbsp;NAME</div>
        <div><i class="fa phone"></i>&nbsp;&nbsp;MOBILE</div>
        <div><i class="fa mobile-phone fa-2"></i>&nbsp;&nbsp;&nbsp;NUMBER</div>
        <div><i class="fa address"></i>&nbsp;&nbsp;&nbsp;XYZ_ADDRESS</div>
    <div class="space">&nbsp;</div>

<div style="padding:10px;padding-left:0px;"><a class="btn btn-primary btn-sm" href="www.link_to_another_page.com"><i class="fa search-plus"></i> &nbsp;more info</a></div>

</div>
<div class="col-lg-2">

</div>
</div>
</div>

我需要的输出是
Heading,NAME,MOBILE,NUMBER,XYZ_ADDRESS

我发现这些数据没有ID或类,但仍以通用文本形式出现在网站中。
为此,我分别尝试使用BeautifulSoup和Python Selenium,在这两种方法中我都被困于提取方法,因为我没有看到任何教程,却引导我从这些方法和标签中提取文本。

我的代码使用BeautifulSoup

import urllib2
from bs4 import BeautifulSoup
import requests
import csv

MAX = 2

'''with open("lg.csv", "a") as f:
  w=csv.writer(f)'''
##for i in range(1,MAX+1)
url="http://www.example_site.com"

page=requests.get(url)
soup = BeautifulSoup(page.content,"html.parser")

for h in soup.find_all('h3'):
    print(h.get('h3'))

我的硒代码

import csv
from selenium import webdriver
MAX_PAGE_NUM = 2
driver = webdriver.Firefox()
for i in range(1, MAX_PAGE_NUM+1):
  url = "http://www.example_site.com"
  driver.get(url)
  name = driver.find_elements_by_xpath('//div[@class = "col-lg-10"]/h3')
  #contact = driver.find_elements_by_xpath('//span[@class="item-price"]')
#  phone = 
#  mobile = 
#  address =
#  print(len(buyers))
#  num_page_items = len(buyers)
#  with open('res.csv','a') as f:
#    for i in range(num_page_items):
#      f.write(buyers[i].text + "," + prices[i].text + "\n")
  print (name)          
driver.close()

参考方案

您可以使用CSS选择器来查找所需的数据。
在您的情况下,div > h3 ~ div将找到直接位于div元素内并以div元素开头的所有h3元素。

import bs4

page= """
<div class="box effect">
<div class="row">
<div class="col-lg-10">
    <h3>HEADING</h3>
    <div><i class="fa user"></i>&nbsp;&nbsp;NAME</div>
    <div><i class="fa phone"></i>&nbsp;&nbsp;MOBILE</div>
    <div><i class="fa mobile-phone fa-2"></i>&nbsp;&nbsp;&nbsp;NUMBER</div>
    <div><i class="fa address"></i>&nbsp;&nbsp;&nbsp;XYZ_ADDRESS</div>
</div>
</div>
</div>
"""

soup = bs4.BeautifulSoup(page, 'lxml')

# find all div elements that are inside a div element
# and are proceeded by an h3 element
selector = 'div > h3 ~ div'

# find elements that contain the data we want
found = soup.select(selector)

# Extract data from the found elements
data = [x.text.split(';')[-1].strip() for x in found]

for x in data:
    print(x)

编辑:刮标题中的文本。

heading = soup.find('h3') 
heading_data = heading.text
print(heading_data)

编辑:或者,您可以使用如下选择器立即获得标题和其他div元素:div.col-lg-10 > *。这将找到属于div类的col-lg-10元素内的所有元素。

soup = bs4.BeautifulSoup(page, 'lxml')

# find all elements inside a div element of class col-lg-10
selector = 'div.col-lg-10 > *'

# find elements that contain the data we want
found = soup.select(selector)

# Extract data from the found elements
data = [x.text.split(';')[-1].strip() for x in found]

for x in data:
    print(x)

Python Pandas导出数据 - python

我正在使用python pandas处理一些数据。我已使用以下代码将数据导出到excel文件。writer = pd.ExcelWriter('Data.xlsx'); wrong_data.to_excel(writer,"Names which are wrong", index = False); writer.…

将python scikit学习模型导出到pmml - python

我想将python scikit-learn模型导出到PMML。哪个python软件包最合适?我阅读了有关Augustus的内容,但是我无法使用scikit-learn模型找到任何示例。 python大神给出的解决方案 SkLearn2PMML是 JPMML-SkLearn命令行应用程序周围的薄包装。有关受支持的Scikit-Learn Estimator和…

Python numpy数据指针地址无需更改即可更改 - python

编辑经过一些摆弄之后,到目前为止,我已经隔离了以下状态:一维数组在直接输入变量时提供两个不同的地址,而在使用print()时仅提供一个地址2D数组(或矩阵)在直接输入变量时提供三个不同的地址,在使用print()时提供两个地址3D数组在直接输入变量时提供两个不同的地址,而在使用print()时仅给出一个(显然与一维数组相同)像这样:>>> …

如何使用python flask从pandas数据框中获取下拉列表项? - python

我需要使用python flask将pandas数据帧中的数据添加到html文档的下拉列表中[email protected]('/api/v1/resources/getservices', methods=['GET']) def api_services(): return render_template('v…

从C#调用javascript函数不起作用 - c#

大家好,我在popupscrit.js文件中使用以下方法:function afficherMessageInfo( id, message) { //Get the A tag alert('executing the client code'); //Get the screen height and width var maskHe…