Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find elements by class

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?

like image 522
Neo Avatar asked Feb 18 '11 11:02

Neo


People also ask

Can I find element by class Selenium Python?

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.

How do you find the element for the tag class and attribute?

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.

How do I find the first element of a class name?

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];


2 Answers

You can refine your search to only find those divs with a given class using BS3:

mydivs = soup.find_all("div", {"class": "stylelistrow"})
like image 65
Klaus Byskov Pedersen Avatar answered Nov 24 '22 00:11

Klaus Byskov Pedersen


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")
like image 21
jmunsch Avatar answered Nov 23 '22 23:11

jmunsch