I\'m having trouble parsing HTML elements with \"class\" attribute using Beautifulsoup. The code looks like this
soup = BeautifulSoup(sdata)
mydivs = soup.fi
Update: 2016 In the latest version of beautifulsoup, the method 'findAll' has been renamed to 'find_all'. Link to official documentation
Hence the answer will be
soup.find_all("html_element", class_="your_class_name")
As of BeautifulSoup 4+ ,
If you have a single class name , you can just pass the class name as parameter like :
mydivs = soup.find_all('div', 'class_name')
Or if you have more than one class names , just pass the list of class names as parameter like :
mydivs = soup.find_all('div', ['class1', 'class2'])
This should work:
soup = BeautifulSoup(sdata)
mydivs = soup.findAll('div')
for div in mydivs:
if (div.find(class_ == "stylelistrow"):
print div
Specific to BeautifulSoup 3:
soup.findAll('div',
{'class': lambda x: x
and 'stylelistrow' in x.split()
}
)
Will find all of these:
<div class="stylelistrow">
<div class="stylelistrow button">
<div class="button stylelistrow">
Try to check if the div has a class attribute first, like this:
soup = BeautifulSoup(sdata)
mydivs = soup.findAll('div')
for div in mydivs:
if "class" in div:
if (div["class"]=="stylelistrow"):
print div
the following worked for me
a_tag = soup.find_all("div",class_='full tabpublist')