"How to fix 'AttributeError: 'NoneType' object has no attribute 'tbody'' error in Python?

"How to fix 'AttributeError: 'NoneType' object has no attribute 'tbody'' error in Python?

我希望在我的桌面目录中创建一个 csv 文件。

导入请求 从 bs4 导入 BeautifulSoup 将 pandas 导入为 pd

url = "https://basketball.realgm.com/ncaa/conferences/Big-12- 
Conference/3/Kansas/54/nba-players"

# get permission
response = requests.get(url)

# access html files
soup = BeautifulSoup(response.text, 'html.parser')

 # creating data frame
columns = ['Player', 'Position', 'Height', 'Weight', 'Draft Year', 'NBA 
Teams', 'Years', 'Games Played','Points Per Game', 'Rebounds Per Game', 
'Assists Per Game']

df = pd.DataFrame(columns=columns)

table = soup.find(name='table', attrs={'class': 'tablesaw','data- 
tablesaw-mode':'swipe','id': 'table-6615'}).tbody

trs = table.find('tr')

# rewording html

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('kansas_player', index=False)

我希望在我的桌面目录中创建一个 csv 文件。

按照你的说法 soup.find(...) 找不到 'table',这可能 为什么你得到 None 类型 returned,这是我的改变,你可以定制它来满足你的 csv 导出需求:

from bs4 import BeautifulSoup
import urllib.request

url = "https://basketball.realgm.com/ncaa/conferences/Big-12-Conference/3/Kansas/54/nba-players"

# get permission
response = urllib.request.urlopen(url)

# access html files
html = response.read()
soup = BeautifulSoup(html)
table = soup.find("table", {"class": "tablesaw"})

此时,您可以return完整的table内容为:

从那里开始,您可以通过以下方式轻松提取 table 行信息:

for tr in table.findAll('tr'):
    tds = tr.find_all('td')
    row = [td.text.replace('\n', '')for td in tds]
    .....

现在每一行看起来像:

最后,您可以将每一行写入 csv 中,带或不带 pandas,您的电话。