johnred
johnred

Reputation: 97

Python3 Find attribute value in html using BeautifulSoup

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)

Upvotes: 2

Views: 53

Answers (1)

Keyur Potdar
Keyur Potdar

Reputation: 7238

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

Upvotes: 1

Related Questions