尝试了解网络抓取功能,目前正在努力从nfl网站上为特定团队(首席人员)抓取团队统计数据。
import requests
from bs4 import BeautifulSoup
data = requests.get('http://www.nfl.com/teams/kansascitychiefs/statistics?
team=KC')
#parser
soup = BeautifulSoup(data.text, 'html.parser')
main = soup.find('div',{'id':'team-stats-wrapper'})
teamstats = soup.find('table',{'id':'data-table1'})
for tbody in teamstats.find('tbody'):
print(tbody)
main和teamstats变量只是用来指定我想从网站使用的html代码的哪一部分。
我在for语句中遇到属性错误,在 AttributeError:“ NoneType”对象没有属性“ find_all”
感谢您的帮助!
答案 0 :(得分:1)
此处没有名为tbody
的属性。如果要打印整个表格。这是代码:
import requests
from bs4 import BeautifulSoup
data = requests.get('http://www.nfl.com/teams/kansascitychiefs/statistics?team=KC')
#parser
soup = BeautifulSoup(data.text, 'html.parser')
main = soup.find('div',{'id':'team-stats-wrapper'})
teamstats_table= main.find('table',{'class':'data-table1 '})
print(teamstats_table)
答案 1 :(得分:0)
import requests
from bs4 import BeautifulSoup
data = requests.get('http://www.nfl.com/teams/kansascitychiefs/statistics?
team=KC')
#parser
soup = BeautifulSoup(data.text, 'html.parser')
main = soup.find_all('div',{'id':'team-stats-wrapper'})
teamstats = soup.find_all('table',{'id':'data-table1'})
for tbody in teamstats.find('tbody'):
print(tbody)