【发布时间】:2019-03-07 08:00:09
【问题描述】:
我是 python 领域的新手,并尝试设置一个网络抓取工具。所以我正在试验一些代码。
import requests
import bs4
website = requests.get("https://www.hltv.org/stats/teams")
soup = bs4.BeautifulSoup(website.text, "html.parser")
leaderboard = soup.find("table", {id: "stats-table player-ratings-table"})
tbody = leaderboard.find("tbody")
for tr in tbody.find.all('tr'):
team = tr.find.all('td')[0].text.strip()
maps = tr.find.all('td')[1].text.strip()
kd = tr.find.all('td')[3].text.strip()
rating = tr.find.all('td')[4].text.strip()
print(team, maps, kd, rating)
我收到以下错误,有什么帮助吗?我用的是 2.7。
File "/Users/*****/Python/New Webscraping/WebS.py", line 11, in <module>
tbody = leaderboard.find("tbody")
AttributeError: 'NoneType' object has no attribute 'find'
Process finished with exit code 1
【问题讨论】:
-
您的
leaderboard对象设置为None,可能是因为find的输出没有返回您所期望的。尝试打印你的汤并检查它有什么问题...... -
你只是混淆了
id和class属性... -
最好复制:stackoverflow.com/questions/34301815/…(上面提到的链接解释了错误的含义,这个链接解释了为什么
find()返回None)。
标签: python web-scraping attributeerror