Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting an element whose id starts with a certain string using BeautifulSoup in python [duplicate]

I am trying to do some web scraping with BS4.

So far I have extracted the <a> using

urls = [item for item in soup.select('h4 a')]

However, I only want to have the urls where the ID starts which entry.

<a href="http://www.sampleurl.com/static/welcome" id="entry_1">Lamborghini </a>

I have tried item.id but it does not work.

What am I missing?

like image 783
user7692855 Avatar asked Aug 08 '26 18:08

user7692855


1 Answers

Use re module together with id.
Here's how:

from bs4 import BeautifulSoup
import re

if __name__ == "__main__":
    html = '<a href="http://www.sampleurl.com/static/welcome" id="entry_1">Lamborghini </a>'
    soup = BeautifulSoup(html, 'html.parser')

    print(soup.find('a', id=re.compile('^entry_')))

output:

<a href="http://www.sampleurl.com/static/welcome" id="entry_1">Lamborghini </a>
like image 138
abdusco Avatar answered Aug 11 '26 08:08

abdusco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!