-1

How do I solve this error?

Code

def sv():
    url = url2
      
    resp=requests.get(url) 

    if resp.status_code==200:
        print("Managed to connect to the website.")
        print("Info :-\n")

        soup=BeautifulSoup(resp.text,'html.parser')    

        l=soup.find("h2",{"class":"etikett bottom-spacing-medium"})
      
        for i in l.findAll("a"):
            print(i.text)
    else:
        print("Error")

sv()

Error

Traceback (most recent call last):
  File "c:\Users\----\Desktop\Python\Biler.py", line 41, in <module>
    sv()
  File "c:\Users\----\Desktop\Python\Biler.py", line 36, in sv
    for i in l.findAll("a"):
AttributeError: 'NoneType' object has no attribute 'findAll'

QUESTION

Ive been stuck on this error for a while. Anyone know how to fix the error.

Eide
  • 1
  • It appears that `soup.find("h2",{"class":"etikett bottom-spacing-medium"})` is returning a NoneType Object which then causes an error when you try to use the .findAll attribute double check the soup.find and the name of the class – Patrick Oct 08 '21 at 11:15

1 Answers1

0

The error message

AttributeError: 'NoneType' object has no attribute 'findAll'

Happens when your object is empty, in this case your code:

        l=soup.find("h2",{"class":"etikett bottom-spacing-medium"})

Returned no results, which would mean either the resulting page didnt have the said object you were trying to find, or the loading wasn't completed successfully, or some other issue caused the page to not be the same.

The easiest thing to do, is to debug the program, grab the value of the webpage at resp.text and compare it to your expected result to find out how the page changed.

Zaid Al Shattle
  • 1,454
  • 1
  • 12
  • 21