AttributeError:"NoneType"对象没有属性"tbody" [英] AttributeError: 'NoneType' object has no attribute 'tbody'
问题描述
我正在尝试创建具有球员数据的Excel表,但是当我尝试抓取特定表时,我会不断获得
I'm trying to create an Excel sheet with players' stats, but when I try to scrape a particular table I keep getting
AttributeError:"NoneType"对象没有属性"tbody".
AttributeError: 'NoneType' object has no attribute 'tbody'.
在这种情况下,我需要Excel工作表上列出的播放器的高级统计信息表.如果高级统计信息不存在,请将该名称旁边的行留空.
In this case, I want the advanced stats table of the listed players on the Excel sheet. If advanced stats don't exist, leave the row empty next to the name.
import requests
import pandas as pd
from bs4 import BeautifulSoup
playernames=['Dominique Jones', 'Joe Young', 'Darius Adams', 'Lester Hudson', 'Marcus Denmon', 'Courtney Fortson']
for name in playernames:
fname=name.split(" ")[0]
lname=name.split(" ")[1]
url="https://basketball.realgm.com/search?q={}+{}".format(fname,lname)
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
table = soup.find('table', attrs={'class':'tablesaw', 'data-table-saw-mode-exclude':'columntoggle'}).tbody
print(table)
columns = ['Season', 'Team', 'League', 'GP', 'GS', 'TS%', 'eFG%', 'ORB%', 'DRB%', 'TRB%', 'AST%', 'TOV%', 'STL%', 'BLK%', 'USG%', 'Total S%', 'PPR', 'PPS', 'ORtg', 'DRtg', 'PER']
df = pd.DataFrame(columns=columns)
trs = table.find_all('tr')
for tr in trs:
tds = tr.find_all('td')
row = [td.text.replace('\n', '') for td in tds]
df = df.append(pd.Series(row, index=columns), ignore_index=True)
df.to_csv('international players.csv', index=False)
推荐答案
我认为您提供了错误的属性名称.这应该data-tablesaw-mode-exclude
不是 data-table-saw-mode-exclude
I think You have provided wrong attribute name.this should data-tablesaw-mode-exclude
Not data-table-saw-mode-exclude
更改此
table = soup.find('table', attrs={'class':'tablesaw', 'data-table-saw-mode-exclude':'columntoggle'}).tbody
为此
table = soup.find('table', attrs={'class': 'tablesaw', 'data-tablesaw-mode-exclude': 'columntoggle'}).find_next('tbody')
这篇关于AttributeError:"NoneType"对象没有属性"tbody"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!