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 of this error?
Luckily, Selenium offers a few methods you can use to find elements. One of the options the Selenium WebDriver library provides for locating HTML elements is the use of the class property. The HTML class property is used for setting a class to an HTML element.
Locate the web element – “Sign in” button. The HTML tag, in this case, is “input”, the attribute is type and value of the attribute is “submit”. Combined, they refer to the “Sign in” button. Type “css=input[type='submit']” (locator value) in Selenium IDE.
If you want only the first element in the DOM with that class, you can select the first element out of the array returned. var elements = document. getElementsByClassName('className'); var requiredElement = elements[0];
You can refine your search to only find those divs with a given class using BS3:
mydivs = soup.find_all("div", {"class": "stylelistrow"})
From the documentation:
As of Beautiful Soup 4.1.2, you can search by CSS class using the keyword argument class_
:
soup.find_all("a", class_="sister")
Which in this case would be:
soup.find_all("div", class_="stylelistrow")
It would also work for:
soup.find_all("div", class_="stylelistrowone stylelistrowtwo")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With