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?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With