将数据从多个div类中刮取到pandas数据帧中

时间:2018-05-21 22:52:44

标签: python pandas beautifulsoup

我正在从仪表板上抓取一些数据,并且试图将多个div classes内的一些数据放入pandas数据框中。我该如何尝试转换这样的东西:

[<div class="map-item" data-companyname="Apical Group" data-country="INDONESIA" data-district="Jakarta Utara" data-latitude="-6.099396000" data-longitude="106.951478000" data-millname="AAJ Marunda" data-province="Jakarta" data-report="http://naturalhealthytreat.com/sites/neste-daemeter.com/files/AAJ_Marunda.pdf" id="map_item_4645">AAJ Marunda</div>,
 <div class="map-item" data-companyname="Apical Group" data-country="INDONESIA" data-district="Lubuk Gaung" data-latitude="1.754005000" data-longitude="101.363532000" data-millname="Sari Dumai Sejati" data-province="Riau" data-report="http://naturalhealthytreat.com/sites/neste-daemeter.com/files/Sari_Dumai_Sejati.pdf" id="map_item_4646">Sari Dumai Sejati</div>,
 <div class="map-item" data-companyname="Kutai Refinery Nusantara " data-country="INDONESIA" data-district="Balikpapan" data-latitude="-1.179099000" data-longitude="116.788274000" data-millname="Kutai Refinery Nusantara " data-province="Penajam Paser Utara" data-report="http://naturalhealthytreat.com/sites/neste-daemeter.com/files/Kutai_Refinery_Nusantara_.pdf" id="map_item_4647">Kutai Refinery Nusantara </div>]

进入这样的数据框:

no  companyname country district    latitude    longitude   millname    province    report
1   Apical Group    INDONESIA   Jakarta Utara   -6.099396   106.951478  AAJ Marunda Jakarta http://naturalhealthytreat.com/sites/neste-daemeter.com/files/AAJ_Marunda.pdf
2   Apical Group    INDONESIA   Lubuk Gaung 1.754005    101.363532  Sari Dumai Sejati   Riau    http://naturalhealthytreat.com/sites/neste-daemeter.com/files/Kutai_Refinery_Nusantara_.pdf
3   Kutai Refinery Nusantara    INDONESIA   Balikpapan  -1.179099   116.788274  Kutai Refinery Nusantara    Penajam Paser Utara http://naturalhealthytreat.com/sites/neste-daemeter.com/files/Kutai_Refinery_Nusantara_.pdf

到目前为止,这是我为了从网页上获取多个div类而编写的代码:

from bs4 import BeautifulSoup
import requests

# Link of Neste dashboard
url = 'http://nestetraceabilitydashboard.com/nestes-pfad-traceability-dashboard' 
page = requests.get(url).content
soup = BeautifulSoup(page, "html.parser")

divList = soup.findAll('div', attrs={ "class" : "map-item"})

1 个答案:

答案 0 :(得分:3)

我们可以使用.attrs属性访问代码的属性。因此,我们可以创建一个包含所有标记属性的字典(class和id除外)并将其传递给数据帧。

from bs4 import BeautifulSoup
import requests
import pandas as pd

url = 'http://nestetraceabilitydashboard.com/nestes-pfad-traceability-dashboard' 
page = requests.get(url).text
soup = BeautifulSoup(page, "html.parser")

divList = soup.findAll('div', attrs={"class" : "map-item"})
data = {}
for div in divList:
    for k,v in div.attrs.items(): 
        if k not in ('class', 'id'):
            k = k.split('-')[1]
            data[k] = data.get(k, []) + [v]

df = pd.DataFrame(data)
print(df.head(3))

数据框:

                 companyname    country       district      latitude  \
0               Apical Group  INDONESIA  Jakarta Utara  -6.099396000
1               Apical Group  INDONESIA    Lubuk Gaung   1.754005000
2  Kutai Refinery Nusantara   INDONESIA     Balikpapan  -1.179099000

       longitude                   millname             province  \
0  106.951478000                AAJ Marunda              Jakarta
1  101.363532000          Sari Dumai Sejati                 Riau
2  116.788274000  Kutai Refinery Nusantara   Penajam Paser Utara

                                              report
0  http://naturalhealthytreat.com/sites/neste-dae...
1  http://naturalhealthytreat.com/sites/neste-dae...
2  http://naturalhealthytreat.com/sites/neste-dae...

有关requests

的建议

最好使用.text代替.content,因为它包含已解码的响应内容(文本)。所以总是使用.text,除非响应a是二进制文件(image,pdf等)。

关于BeautifulSoup

findAll用于与bs3向后兼容,bs4将其重命名为find_all。此外,您可以将属性用作keyword arguments,例如:

find_all('div', class_="map-item")

这里我使用class_作为类属性,因为class是Python关键字,但对于其他属性(id,href等),您可以按原样使用它们的名称。