I'm trying to get the data from a combobox using beautifulsoup, but when using my code I get the following error:
AttributeError: 'NoneType' object has no attribute 'get'
DATA:
<select name="sexo" style="width: 140px; display: none;" id="sexo" tabindex="0" aria-disabled="false">
<option selected="" value="">[SELECCIONAR]</option>
<option value="M" selected="">Masculino</option> <<<<SELECTED ITEM
<option value="F">Femenino</option>
</select>
My code:
sexo = BeautifulSoup(login_request.text, 'lxml').find('input', {'name': 'sexo'}).get('value')
print(sexo)
How can i get only the selected value?
CodePudding user response:
The element you want to search for is select
, not input
. And if you want the selected value, you have to search within that for the option
with the selected
attribute.
You can combine these into a single call using a selector.
sexo = BeautifulSoup(login_request.text, 'lxml').select('select[name=sexo] option[selected]')[0].value
This probably isn't going to be very useful, since the selected value is the empty value that goes with the [SELECCIONAR]
prompt. You're not getting anything that a user has selected.