![](/img/trans.png)
[英]BeautifulSoup "AttributeError: 'NoneType' object has no attribute 'text'"
[英]'NoneType' object has no attribute 'text' | Beautifulsoup
我剛剛開始學習 python webscraping,我想學習如何從 NFL 網站抓取數據以顯示所有球員及其統計數據,但我在 Beautifulsoup 中出現了這個錯誤。
import requests
from bs4 import BeautifulSoup
url = "https://www.pro-football-reference.com/years/2021/passing.htm"
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
league_table = soup.find('table', class_ = 'per_match_toggle sortable stats_table')
for name in league_table.find_all('tbody'):
rows = name.find_all('tr')
for row in rows:
name = row.find('td', class_ = 'left').text.strip()
yards = row.find_all('td', class_ = 'right')[7].text
touchdowns = row.find_all('td', class_ = 'right')[8].text
print("Name " + name + " Yards " + yards + " Touchdowns " + touchdowns)
錯誤:
name = row.find('td', class_ = 'left').text.strip()
發生這種情況是因為find()
可以返回None
,顯然,它沒有text
屬性。
當您要搜索的元素不存在或您將錯誤的參數傳遞給搜索函數時,就會發生這種情況。
您應該用try-except
子句或if else
包裝有問題的部分,以處理此類情況
發生這種情況是因為您會注意到在 James Winston 之后,有一排標題。 所以<tr>
標簽由<th>
標簽組成,而不是<td>
標簽。 所以它到達那一行,你說.find('td')
,它不包含所以它返回None
。 然后你想從中獲取文本,你從None
得到.text
。
因此,您需要像上一篇文章所建議的那樣,使用 try/except 或僅采用帶有<td>
標簽的行的邏輯。
就個人而言,我只是使用 Pandas 來抓取表格,刪除該標題行,然后遍歷這些行。
import pandas as pd
url = "https://www.pro-football-reference.com/years/2021/passing.htm"
df = pd.read_html(url)[0]
df = df[df['Player'].ne('Player')]
for idx, row in df.iterrows():
name = row['Player']
yards = row['Yds']
touchdowns = row['TD']
print("Name " + name + " Yards " + yards + " Touchdowns " + touchdowns)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.