Python BeautifulSoup: wildcard attribute/id search

Question:

I have this:

dates = soup.findAll("div", {"id" : "date"})

However, I need id to be a wildcard search since the id can be date_1, date_2 etc.

Asked By: user984003

||

Answers:

You can provide a callable as a filter:

dates = soup.findAll("div", {"id" : lambda L: L and L.startswith('date')})

Or as @DSM points out

dates = soup.findAll("div", {"id" : re.compile('date.*')})

as BeautifulSoup will recognise a RegExp object and call its .match() method.

Answered By: Jon Clements
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.