Matching partial ids in BeautifulSoup

Max Frai picture Max Frai · May 13, 2010 · Viewed 19.3k times · Source

I'm using BeautifulSoup. I have to find any reference to the <div> tags with id like: post-#.

For example:

<div id="post-45">...</div>
<div id="post-334">...</div>

I have tried:

html = '<div id="post-45">...</div> <div id="post-334">...</div>'
soupHandler = BeautifulSoup(html)
print soupHandler.findAll('div', id='post-*')

How can I filter this?

Answer

Mark Byers picture Mark Byers · May 13, 2010

You can pass a function to findAll:

>>> print soupHandler.findAll('div', id=lambda x: x and x.startswith('post-'))
[<div id="post-45">...</div>, <div id="post-334">...</div>]

Or a regular expression:

>>> print soupHandler.findAll('div', id=re.compile('^post-'))
[<div id="post-45">...</div>, <div id="post-334">...</div>]