使用beautifulSoup,Python在h3和div标签中刮取文本 [英] Scraping text in h3 and div tags using beautifulSoup, Python

查看:261
本文介绍了使用beautifulSoup,Python在h3和div标签中刮取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have no experience with python, BeautifulSoup, Selenium etc. but I'm eager to scrape data from a website and store as a csv file. A single sample of data I need is coded as follows (a single row of data).

<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,在这两种方法中我都被困于提取方法,因为我没有看到任何教程,却引导我从这些方法和标签中提取文本

I found those data don't have a id or class yet being in website as general text. I'm trying BeautifulSoup and Python Selenium separately for that, where I got stuck to extract in both the methods as no tutorials I saw, guided me to extract text from these and tags

我的代码使用BeautifulSoup

My code using 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元素内并以h3元素开头的所有div元素.

You can use CSS selectors to find the data you need. In your case div > h3 ~ div will find all div elements that are directly inside a div element and are proceeded by a h3 element.

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 > *.这样会在col-lg-10类的div元素内找到所有元素.

Or you can get the heading and other div elements at once by using a selector like this: div.col-lg-10 > *. This finds all elements inside a div element that belongs to col-lg-10 class.

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)

这篇关于使用beautifulSoup,Python在h3和div标签中刮取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆