Matching partial ids in BeautifulSoup

Solution 1:

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>]

Solution 2:

Since he is asking to match "post-#somenumber#", it's better to precise with

import re
[...]
soupHandler.findAll('div', id=re.compile("^post-\d+"))

Solution 3:

soupHandler.findAll('div', id=re.compile("^post-$"))

looks right to me.