2

Supose you have a web page with a lot of this:

<div class="story cid-8797378263432 l-es headline-story thumbnail-true">

where cid-nnnnnnnnnnnn class can vary. How would you get all the divs with BeautifulSoup?

I tried:

soup.find('div', {'class': 'story'})

but that didn't work. Seems to look for the divs with ONLY the story class.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Juanjo Conti
  • 28,823
  • 42
  • 111
  • 133
  • http://stackoverflow.com/questions/1242755/beautiful-soup-cannot-find-a-css-class-if-the-object-has-other-classes-too/1242801#1242801 – endolith Nov 30 '11 at 15:12

2 Answers2

1

Or you can just use soup.findAll('div', 'story') which doesn't seem to have that bug.

Brandon O'Rourke
  • 24,165
  • 16
  • 57
  • 58
0

It's a bug: Launchpad bug report. The report also contains a workaround:

soup.findAll('div', {'class': re.compile(r'\bstory\b')})
Boldewyn
  • 81,211
  • 44
  • 156
  • 212