我没有使用 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> NAME</div>
<div><i class="fa phone"></i> MOBILE</div>
<div><i class="fa mobile-phone fa-2"></i> NUMBER</div>
<div><i class="fa address"></i> XYZ_ADDRESS</div>
<div class="space"> </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> 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()
原文由 Revaapriyan 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以使用 CSS 选择器来查找您需要的数据。 In your case
div > h3 ~ div
will find alldiv
elements that are directly inside adiv
element and are proceeded by ah3
element.编辑:要抓取标题中的文字..
编辑:或者您可以使用这样的选择器一次获取标题和其他 div 元素:
div.col-lg-10 > *
。这将找到div
属于col-lg-10
类的元素内的所有元素。