0

Good evening my dear community. My question of tonight is following: I have this piece of HTML:

                      <option class="disabled" value="12_654" >
(EU 38 2/3 - US 6)                      </option>
                      <option class="disabled" value="12_3716" >
(EU 39 1/3 - US 6,5)                      </option>
                      <option class="disabled" value="12_636" >
(EU 40 - US 7)                      </option>
                      <option class="" value="12_634" >
EU 40 2/3 - US 7,5                      </option>
                      <option class="" value="12_462" >
EU 41 1/3 - US 8                      </option>
                      <option class="" value="12_460" >
EU 42 - US 8,5                      </option>
                      <option class="" value="12_459" >
EU 42 2/3 - US 9                      </option>
                      <option class="" value="12_458" >
EU 43 1/3 - US 9,5                      </option>
                      <option class="" value="12_457" >
EU 44 - US 10                      </option>
                      <option class="" value="12_456" >
EU 44 2/3 - US 10,5                      </option>
                      <option class="" value="12_455" >
EU 45 1/3 - US 11                      </option>
                      <option class="disabled" value="12_559" >
(EU 46 - US 11,5)                      </option>
                      <option class="disabled" value="12_454" >
(EU 46 2/3 - US 12)                      </option>
                      <option class="disabled" value="12_453" >
(EU 47 1/3 - US 12,5)                      </option>
                    </sel

it is just an example.... My goal is now to find the value of a specific size, by just giving the size. For example:

If I indicate my size is EU 40 - US 7 I want to be able to parse the value 12_636

I previously already used the bs4 module to parse HTML and to find specific values and I usually do it like this:

from bs4 import BeautifulSoup as bs

soup=bs(html.text, 'lxml')
v=soup.find('option',{'class':''})['value']

but here, because with the method above there would be more possible values, it is not what I need. I tried about adding the desidered size in the tag I want to find, but it did not work.

v=soup.find('option',(EU 40 - US 7),{'class':''})['value']

It looked like this, but yes I know, it is incorrect. Now I have absolutely no idea on how to find the value, so I would be really grateful on any help I get. Thanks a lot dear community!

2 Answers 2

1

Create a dict mapping each option's text to its value, making sure to strip all the extraneous whitespace from text:

d = {option.text.strip(): option['value'] for option in soup.find_all('option')}
print(d['(EU 40 - US 7)'])

Result:

12_636
Sign up to request clarification or add additional context in comments.

1 Comment

This works for me, thanks for taking the time for answering. Appreciate it a lot!
1

you could also use xpath and fromstring.

# import requests
from lxml.html import fromstring
# url = ''
# tree = html.fromstring( requests.get(url).content)
h = '''
 <option class="disabled" value="12_654" >
(EU 38 2/3 - US 6)                      </option>
                      <option class="disabled" value="12_3716" >
(EU 39 1/3 - US 6,5)                      </option>
                      <option class="disabled" value="12_636" >
(EU 40 - US 7)                      </option>
                      <option class="" value="12_634" >
EU 40 2/3 - US 7,5                      </option>
                      <option class="" value="12_462" >
EU 41 1/3 - US 8                      </option>
                      <option class="" value="12_460" >
EU 42 - US 8,5                      </option>
                      <option class="" value="12_459" >
EU 42 2/3 - US 9                      </option>
                      <option class="" value="12_458" >
EU 43 1/3 - US 9,5                      </option>
                      <option class="" value="12_457" >
EU 44 - US 10                      </option>
                      <option class="" value="12_456" >
EU 44 2/3 - US 10,5                      </option>
                      <option class="" value="12_455" >
EU 45 1/3 - US 11                      </option>
                      <option class="disabled" value="12_559" >
(EU 46 - US 11,5)                      </option>
                      <option class="disabled" value="12_454" >
(EU 46 2/3 - US 12)                      </option>
                      <option class="disabled" value="12_453" >
(EU 47 1/3 - US 12,5)                      </option>
                    </sel
'''
tree = fromstring(h)
print(tree.xpath("//option[text()[contains(.,'(EU 40 - US 7)')]]/@value"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.