BeautifulSoup find_all that 'Kind of match'

Viewed 81

I want to scrape a website and I need to get all the tags that match the first characters of my search string.

so basically there are a few divs like this:

<div class="" style="" id="uni-item-229925"></div>
<div class="" style="" id="uni-item-223625"></div>
<div class="" style="" id="uni-item-229325"></div>

and i wann geht all the divs that start with uni-item no matter what their ending number is.

If they're all the same Ii would use this:

items = soup.find_all('div', {"class": "uni-item"})

But for this specific Use case I'm clueless any help is much appreciated

2 Answers

I think John Clements answered this here beautifully (pun intended). https://stackoverflow.com/a/14257743/16068811

So in your case:

items = soup.findAll("div", {"id" : re.compile('uni-item.*')})

or

items = soup.findAll("div", {"id" : lambda L: L and L.startswith('uni-item')})

haven't tried it but should work.

You can do that easily using css selectors as follows:

items = soup.select('div[#id^ = "uni-item"]')
Related