不了解此属性错误的原因:"NoneType"对象没有属性"find_all"


import requests
from bs4 import BeautifulSoup
url="https://ratings.fide.com/top_lists.phtml"
html = requests.get(url).content
soup = BeautifulSoup(html,"html.parser")
list = soup.find("tbody").find_all("tr",limit=10)
count = 1
for tr in list:
title = tr.find("td").find("a").text
_count = str(count)+"."
print(f"{_count.ljust(3)} Oyuncu Adı:{title.ljust(55)}")
count += 1

它给出了:AttributeError: 'NoneType' object has no attribute 'find_all'

为什么这个代码不起作用?

页面的内容是通过Ajax调用加载的。将URL页面更改为a_top.php
import requests
from bs4 import BeautifulSoup
url="https://ratings.fide.com/a_top.php"
html = requests.get(url).content
soup = BeautifulSoup(html, "html.parser")
table = soup.find('table')
for row in table.find_all('tr'):
print(row)

请参阅Beautifulsoup和AJAX表问题

来自BeautifulSoup网站:

AttributeError:"NoneType"对象没有属性"foo"-这通常是因为您调用了find((,然后试图访问结果的.foo属性。但是在您的例子中,find((没有找到任何东西,所以它返回None,而不是返回一个标记或字符串。您需要弄清楚为什么find((调用没有返回任何内容。

看起来您正在抓取的网页没有使用tbody标记来显示其数据。

最新更新