I'm having trouble parsing html elements with "class" attribute using Beautifulsoup. The code looks like this
soup = BeautifulSoup(sdata)
mydivs = soup.findAll('div')
for div in mydivs:
if (div["class"]=="stylelistrow"):
print div
I get an error on the same line "after" the script finishes.
File "./beautifulcoding.py", line 130, in getlanguage
if (div["class"]=="stylelistrow"):
File "/usr/local/lib/python2.6/dist-packages/BeautifulSoup.py", line 599, in __getitem__
return self._getAttrMap()[key]
KeyError: 'class'
How do I get rid or this error?
From the documentation:
As of Beautiful Soup 4.1.2, you can search by CSS class using the keyword argument class_:
Which in this case would be:
It would also work for:
This works for me to access the class attribute (on beautifulsoup 4, contrary to what the documentation says). The KeyError comes a list being returned not a dictionary.
You can refine your search to only find those divs with a given class using BS3:
Try to check if the div has a class attribute first, like this:
This worked for me:
A straight forward way would be :
Make sure you take of the casing of findAll, its not findall