From what I can see of the html, there is no span
with id="sexo- button"
, so BeautifulSoup(login_request.text, 'lxml').find("span",id="sexo- button")
would have returned None
, which is why you got the error from get_text
.
As for your second attempt, I don’t think bs4 Tags have a value
property, which is why you’d be getting None
that time.
You should actually try combining the two like:
sexo = BeautifulSoup(login_request.text, 'lxml').select_one('select[name=sexo] option[selected]').get_text(strip=True)
(If it doesn’t work, you should individually print to see what ...select_one('select[name=sexo]')
and ...select_one('select[name=sexo] option[selected]')
will return, in case the page itself wasn’t loaded properly by session
)
You should also note that with the combined code, you’ll actually get [SELECCIONAR]
, since that’s also selected according to the html provided. To skip that, you can instead try:
selectedOpts = BeautifulSoup(login_request.text, 'lxml').select('select[name=sexo] option[selected]')
selectedOpts = [s for s in selectedOpts if s.get('value')]
sexo = selectedOpts[0] if selectedOpts else None
3
solved How to get data from a combobox using Beautifulsoup and Python?