[英]Python Attribute Error: 'NoneType' object has no attribute 'find_all'
[英]Python error: 'NoneType' object has no attribute 'find_all'
我正在改編http://danielfrg.com/blog/2013/04/01/nba-scraping-data/#disqus_thread中的網絡抓取程序,以將棒球數據的ESPN抓取為CSV。 但是,當我運行第二段代碼來編寫游戲的csv時,從下面的代碼部分中得到“ NoneType”對象沒有屬性“ find_all”錯誤
for index, row in teams.iterrows():
_team, url = row['team'], row['url']
r = requests.get(BASE_URL.format(row['prefix_1'], year, row['prefix_2']))
table = BeautifulSoup(r.text).table
for row in table.find_all("tr")[1:]: # Remove header
columns = row.find_all('td')
try:
_home = True if columns[1].li.text == 'vs' else False
_other_team = columns[1].find_all('a')[1].text
_score = columns[2].a.text.split(' ')[0].split('-')
_won = True if columns[2].span.text == 'W' else False
match_id.append(columns[2].a['href'].split('?id=')[1])
home_team.append(_team if _home else _other_team)
visit_team.append(_team if not _home else _other_team)
d = datetime.strptime(columns[0].text, '%a, %b %d')
dates.append(date(year, d.month, d.day))
我可以發布整個程序,但這是編譯器為其讀取錯誤的代碼。
完整的錯誤文本為
Traceback (most recent call last):
File "C:\Python27\Project Files\Game Parser.py", line 23, in <module>
for row in table.find_all("tr")[1:]: # Remove header
AttributeError: 'NoneType' object has no attribute 'find_all'
任何有關如何使此代碼運行的幫助將不勝感激。
該錯誤意味着您正在通過執行以下操作來構建table
變量:
table = BeautifulSoup(r.text).table
返回None
。 for row in table.find_all("tr")[1:]:
,在None
上拋出錯誤。
您可以檢查所涉及的url
是否具有嘗試訪問它的方式的表。 您可以通過打印以下語句構造的url
來做到這一點:
BASE_URL.format(row['prefix_1'], year, row['prefix_2'])
然后在瀏覽器中轉到該網址,以查看其中是否有您感興趣的表格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.