Can't select by CSS class if element has more than one class

Bug #410304 reported by Endolith
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Beautiful Soup
Fix Released
Undecided
Unassigned

Bug Description

If a page has <p class="class1">, then soup.findAll('p', 'class1') will find it.

If it has <p class="class1 class2">, though, it will not be found. BeautifulSoup treats this as a single class with a space in it 'class1 class2' rather than two classes ['class1','class2'].

A workaround is to use a regular expression to search for the class instead of a string:

soup.findAll('p', {'class': re.compile(r'\bclass1\b')})

But I think it should understand that an object has multiple classes.

Revision history for this message
MORITA Hajime (morrita) wrote :

Hi,
I made a patch to try fixing this problem.
In the patch, attribute name prefixed by "@" means HTML class-like search,
for example, findAll("span", {"@class": "foo"}) will work as such.

Revision history for this message
MORITA Hajime (morrita) wrote :

Oops, the patch is totally irrelevant! I've removed that. Sorry to disturb you...

Revision history for this message
Endolith (endolith) wrote :

(I've just been using LXML instead.)

Revision history for this message
Leonard Richardson (leonardr) wrote :

Beautiful Soup 4 beta 5 deals with multi-valued attributes (of which 'class' is the most common) a lot better, and also improves searching by CSS class.

Revision history for this message
Leonard Richardson (leonardr) wrote :

I'm satisfied with the behavior of beta 6 w/r/t searching by CSS class. There's still a small hack, but I can get rid of the hack without changing the API.

Changed in beautifulsoup:
status: New → Fix Committed
Changed in beautifulsoup:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.