2017-10-25 25 views
0

私はPython、BeautifulSoup、Seleniumなどの経験はありませんが、Webサイトからデータをスクレイプしてcsvファイルとして保存したいと思っています。 必要なデータのサンプルは、次のようにコード化されています(1行のデータ)。beautifulSoup、Pythonを使用してh3タグとdivタグのテキストをスクラビング

<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> 

私は必要な出力は、私はそれらのデータは、IDまたはクラスがまだ一般的なテキストとしてウェブサイト内にある必要はありません見つけ Heading,NAME,MOBILE,NUMBER,XYZ_ADDRESS

です。 私は私が見たノーチュートリアルとして、両方の方法で抽出するために捕まってしまったところ、私は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')) 
を使用して、これらとタグ

私のコードからテキストを抽出するために私を導い、そのために別々にBeautifulSoupとPythonセレンをしようとしています

マイセレンコード

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() 

答えて

1

あなたが必要とするデータを見つけるために、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 > *。これにより、col-lg-10クラスに属するdiv要素内のすべての要素が検索されます。

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) 
0

これを試してみてください:

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,"html.parser") 

print(soup.text) 
関連する問題