Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python BeautifulSoup findAll by "class" attribute

I want to do the following code, which is what BS documentation says to do, the only problem is that the word "class" isn't just a word. It can be found inside HTML, but it's also a python keyword which causes this code to throw an error.

So how do I do the following?

soup.findAll('ul', class="score")
like image 684
appleLover Avatar asked Nov 14 '13 03:11

appleLover


People also ask

What is the difference between find and Findall in BeautifulSoup?

find is used for returning the result when the searched element is found on the page. find_all is used for returning all the matches after scanning the entire document.


2 Answers

Your problem seems to be that you expect find_all in the soup to find an exact match for your string. In fact:

When you search for a tag that matches a certain CSS class, you’re matching against any of its CSS classes:

You can properly search for a class tag as @alKid said. You can also search with the class_ keyword arg.

soup.find_all('ul', class_="score")
like image 195
mattexx Avatar answered Sep 19 '22 16:09

mattexx


Here is how to do it:

soup.find_all('ul', {'class':"score"})
like image 33
aIKid Avatar answered Sep 17 '22 16:09

aIKid