0

I was wondering if there a way for me to search for the first few characters of an id. Eg using the find(id=''), if an item id was 'priceblock_ourprice' could I search just for items with the id starting with 'priceblock'?

I have searched for ways to do this but my searches have been fruitful. and nothing i have tried has worked. Maybe something like this would work:

soup.find(id[0:9]="priceblock")

of course this didn't work but I was hoping someone has a fix, thanks in advance <3

Micheal Nestor
  • 91
  • 1
  • 10
  • Check answer here https://stackoverflow.com/questions/2830530/matching-partial-ids-in-beautifulsoup – L. Letovanec Nov 04 '19 at 09:39
  • Just as reminder - you can always solve such kind of simple tasks in plain Python. BeautifulSoup solves for you all HTML-related problems, next your can simply iterate across all IDs with regular expressions, custom functions and so on. – QtRoS Nov 04 '19 at 09:48

1 Answers1

2

You can use select_one with css selector, [id^='priceblock'] means id starts with priceblock:

soup.select_one("[id^='priceblock']")
Sers
  • 12,047
  • 2
  • 12
  • 31