Reputation: 55
I've got a problem using BeautifulSoup. I need to get the value of the element using only some text inside but I've got no idea how to do that. There's some HTML code:
<option class="" value="9_72"> 42,5 EUR · 9 US </option>
<option class="" value="9_73"> 43 EUR · 9,5 US </option>
<option class="" value="9_74"> 44 EUR · 10 US </option>
Is there any posibility to get it?
I tried this code:
scraper = cfscrape.create_scraper()
content_rdy = scraper.get(link, headers=headers).text
soup = bs4.BeautifulSoup(content_rdy, 'lxml')
input_id = soup.find_all('option', text='44 EUR')
print(input_id)
but it gives me empty []
. If I use:
input_id = soup.find_all('option')
I can't get exact the value of the option. And the problem is that the only thing how I can get this value is the size inside this element.
I want to get this -> value="9_72"
Upvotes: 0
Views: 246
Reputation: 5370
from bs4 import BeautifulSoup as bs
html = """
<option class="" value="9_72"> 42,5 EUR · 9 US </option>
<option class="" value="9_73"> 43 EUR · 9,5 US </option>
<option class="" value="9_74"> 44 EUR · 10 US </option>"""
soup = bs(html, 'html.parser')
options = [option for option in soup.find_all('option') if '44 EUR' in option.text]
values = [option.get("value") for option in options]
it returns all tags, who text contains "44 EUR" string
Upvotes: 1
Reputation: 1834
Add this after the line:
input_id = soup.find_all('option')
for options in input_id:
option_text = options.text
if '44 EUR' in options_text:
final_options_text = options_text
print(final_options_text)
Essentially you can iterate over the result set and do a wild card match to see which of the tags has the text you need.
Upvotes: 0