YUSUFAKCAY
YUSUFAKCAY

Reputation: 1

Don't understand the cause of this AttributeError: 'NoneType' object has no attribute '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

And it gives: AttributeError: 'NoneType' object has no attribute 'find_all'

Why this code doesnt work?

Upvotes: 0

Views: 157

Answers (2)

hpelleti
hpelleti

Reputation: 342

The content of the page is load through an Ajax call. Change the URL page to 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)

See Beautifulsoup and AJAX-table problem

Upvotes: 1

ProjectTermina
ProjectTermina

Reputation: 26

From the BeautifulSoup website:

AttributeError: 'NoneType' object has no attribute 'foo' - This usually happens because you called find() and then tried to access the .foo attribute of the result. But in your case, find() didn’t find anything, so it returned None, instead of returning a tag or a string. You need to figure out why your find() call isn’t returning anything.

It looks like the web page you're grabbing doesn't use the tbody tag to display its data.

Upvotes: 0

Related Questions