簡體   English   中英

使用Python / BeautifulSoup刮網站 - 為什么這個表返回None?

[英]Scraping website using Python/BeautifulSoup — why does this Table return None?

我試圖從這張表中刪除數據:: http://www.worldlifeexpectancy.com/cause-of-death/alzheimers-dementia/by-country/我想找的元素是國家的名字,在這種情況下芬蘭:

<table cellspacing="0" align="center" class="hc_tbl">
<tbody>
<tr>
<td class="hc_name" style="background-color: transparent;">Finland</td>

這是我正在使用的代碼:

res = requests.get('http://www.worldlifeexpectancy.com/cause-of-death/alzheimers-dementia/by-country/')

soup = BeautifulSoup(res.content, 'html5lib')

table = soup.find('table', {'class': 'hc_tbl'})

for row in table.find('tbody').find_all('tr'):
    name = row.find('td', {'class':'hc_name'}).text.strip()
    print (name)

但是,這會產生一個錯誤,即“NoneType”對象沒有屬性“find”;; 因此看起來表元素被返回為“無”。

我已經閱讀了一些似乎有類似問題的其他帖子,但在這種情況下沒有一個修復工作。

任何想法都非常感謝

謝謝

通過在發送請求時檢查站點的來源,可以看出該站點是動態的。 因此,最好使用瀏覽器操作工具,如selenium

from bs4 import BeautifulSoup as soup 
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get('http://www.worldlifeexpectancy.com/cause-of-death/alzheimers-dementia/by-country/')
countries = filter(None, [i.text for i in soup(driver.page_source, 'lxml').find_all('td', {'class':'hc_name'})])

輸出:

[u'Finland', u'Djibouti', u'North Korea', u'United States', u'Gabon', u'Venezuela', u'Canada', u'Estonia', u'Zambia', u'Iceland', u'Guyana', u'Russia', u'Sweden', u'Senegal', u'Burundi', u'Switzerland', u'Jordan', u'Eritrea', u'Norway', u'Mali', u'Central Africa', u'Denmark', u'Namibia', u'DR Congo', u'Netherlands', u'Romania', u'Somalia', u'Belgium', u'Moldova', u'Pakistan', u'Spain', u'Bahrain', u'Bolivia', u'Australia', u'Panama', u'Tunisia', u'France', u'Ghana', u'Bhutan', u'United Kingdom', u'Mexico', u'Syria', u'Cuba', u'Sierra Leone', u'Turkey', u'Chile', u'Mauritania', u'Nicaragua', u'Uruguay', u'Tanzania', u'Egypt', u'Israel', u'Sri Lanka', u'Madagascar', u'New Zealand', u'Poland', u'Bosnia/Herzeg.', u'Ireland', u'Benin', u'Lebanon', u'Italy', u'Mozambique', u'Ethiopia', u'Hungary', u'Belize', u'Nepal', u'Malta', u'Nigeria', u'Guatemala', u'Luxembourg', u'Montenegro', u'Ukraine', u'Germany', u'Angola', u'Paraguay', u'Brazil', u'Gambia', u'Colombia', u'South Korea', u'Uganda', u'Bangladesh', u'Cyprus', u'New Guinea', u'Saudi Arabia', u'Costa Rica', u'Slovakia', u'Philippines', u'Iran', u'Guinea-Bissau', u'Indonesia', u'South Africa', u'Burkina Faso', u'Slovenia', u'Austria', u'Cote d Ivoire', u'Honduras', u'Serbia', u'Chad', u'Armenia', u'Trinidad/Tob.', u'Morocco', u'Peru', u'Bahamas', u'Comoros', u'Thailand', u'Maldives', u'Guinea', u'El Salvador', u'Portugal', u'Kenya', u'Yemen', u'Latvia', u'Greece', u'Myanmar', u'Czech Republic', u'Zimbabwe', u'Bulgaria', u'Argentina', u'Viet Nam', u'Turkmenistan', u'Qatar', u'Belarus', u'Malaysia', u'Solomon Isl.', u'Kazakhstan', u'Macedonia', u'Croatia', u'Rwanda', u'Laos', u'Swaziland', u'Niger', u'Mongolia', u'Arab Emirates', u'Togo', u'Timor-Leste', u'Fiji', u'Dominican Rep.', u'Afghanistan', u'Haiti', u'South Sudan', u'Kuwait', u'Equ. Guinea', u'Malawi', u'Azerbaijan', u'Cape Verde', u'Ecuador', u'India', u'Lesotho', u'Brunei', u'Cambodia', u'Jamaica', u'Congo', u'Tajikistan', u'Botswana', u'Albania', u'Kyrgyzstan', u'China', u'Sudan', u'Uzbekistan', u'Barbados', u'Oman', u'Georgia', u'Iraq', u'Mauritius', u'Singapore', u'Lithuania', u'Algeria', u'Suriname', u'Cameroon', u'Liberia', u'Japan', u'Libya']

該表在頁面源中不可用。 它使用AJAX請求動態加載。 如果您查看Developer工具下的Network選項卡,則會向此url發出AJAX請求 - http://www.worldlifeexpectancy.com/j/country-cause?cause=95&order=hight

您可以看到數據以JSON格式提供。 在內置的.json()函數的幫助下,您可以僅使用requests模塊來抓取此數據。

您可以從此JSON數據中獲取所有數據,例如排名,國家/地區和費率。

import requests

r = requests.get('http://www.worldlifeexpectancy.com/j/country-cause?cause=95&order=hight')
data = r.json()

for row in data['chart']['countries']['countryitem']:
    id_ = row['id']
    country = row['name']
    rank = row['rank']
    value = row['value']
    print(rank, id_, country, value)

部分輸出:

1 FI Finland 53.77
2 US United States 45.58
3 CA Canada 35.50
4 IS Iceland 34.08
5 SE Sweden 32.41
6 CH Switzerland 32.25
...
...

另外,請記住<tbody>元素在頁面源中永遠不可用。 瀏覽器插入它。 因此,在抓取表時,不要在find()函數中使用tbody 請參閱為什么瀏覽器將tbody元素插入表元素?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM