2

I'm trying to parse:

<form id="main">
    <select {disable} id="TRANSPORT" style="font-size: 8pt;margin:0;width:250px;">
        <option selected value="TCP">
            TCP(selected)
        </option>
        <option value="UDP">
            UDP
        </option>
    </select>
</form>

selected value for the TRANSPORT. and I can't figure out how to do it. what I tried:

from bs4 import BeautifulSoup
f = '''<form id="main">\n
<select {disable} id="TRANSPORT"  style="font-size: 
8pt;margin:0;width:250px;"><option selected value="TCP">TCP(selected) 
</option><option value="UDP">UDP</option></select>
</form>>'''
soup = BeautifulSoup(f, "lxml")
last_tag = soup.find("select",id ="TRANSPORT")
TRANSPORT_ID = last_tag.get('id')
TRANSPORT_VAL = last_tag.get('selected value')
print(TRANSPORT_ID, TRANSPORT_VAL)

I get the result:

TRANSPORT None

but I must get the result:

TRANSPORT TCP

because - TCP(selected)

0

1 Answer 1

1

selected value is not an attribute of the <option> tag. It simply shows that the current attribute selected has no value (but the attribute is present), and the attribute you are looking for is value="TCP".

Or, to explain it better, selected value="TCP" is the same as selected="" value="TCP".

So, if you want to find the <option> tag which is selected, you can use find('option', selected=True) and get the value using get('value').

f = '''
<form id="main">
    <select {disable} id="TRANSPORT" style="font-size: 8pt;margin:0;width:250px;">
        <option selected value="TCP">
            TCP(selected)
        </option>
        <option value="UDP">
            UDP
        </option>
    </select>
</form>'''
soup = BeautifulSoup(f, "lxml")

transport = soup.find('select', id='TRANSPORT')
transport_id = transport['id']
transport_value = transport.find('option', selected=True)['value']
print(transport_id, transport_value)
# TRANSPORT TCP
Sign up to request clarification or add additional context in comments.

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.