Search code examples
pythonpython-3.xbeautifulsoupattributeerror

Python Attribute Error: 'NoneType' object has no attribute 'find_all'


I'm trying to get abbreviations of US states but this code:

from bs4 import BeautifulSoup
from urllib.request import urlopen
url='https://simple.wikipedia.org/wiki/List_of_U.S._states'
web=urlopen(url)
source=BeautifulSoup(web, 'html.parser')
table=source.find('table', {'class': 'wikitable sortable jquery-tablesorter'})
abbs=table.find_all('b')
print(abbs.get_text())

returns AttributeError: 'Nonetype' object has no attribute 'find_all'. What's the problem of my code?


Solution

  • As suggested in the comments the HTML at the url doesn't have a table with the class

    'wikitable sortable jquery-tablesorter'
    

    But the class is actually

    'wikitable sortable'
    

    Also once you apply find_all, it returns a list containing all tags so you can't directly apply get_text() to it. You can use list comprehension to strip out the text for each element in the list. Here's the code which will work for your problem

    from bs4 import BeautifulSoup
    from urllib.request import urlopen
    url='https://simple.wikipedia.org/wiki/List_of_U.S._states'
    web=urlopen(url)
    source=BeautifulSoup(web, 'html.parser')
    table=source.find('table', {'class': 'wikitable sortable'})
    abbs=table.find_all('b')
    values = [ele.text.strip() for ele in abbs]
    print(values)