Python - non e ' in grado di estrarre valore di input nascosto

0

Domanda

Sto cercando di estrarre il valore di un input nascosto tag. Anche se l'elemento esiste in HTML non riesco a trovare con bs4.

Questo è il messaggio di errore che mi esce:

AttributeError: 'NoneType' object has no attribute 'find'

Questo è il codice html della pagina web:

<form id="exampleid" class="exampleclass" action="/ex/ex-ex/ex/2" method="post">
    
    <more html>
                                
    <div>
    <input type="hidden" name="csrf" value="abcdefghijklmnopqrstuvwxyz">
    </div></form>

E questo è il mio codice attuale:

csrf = soup.find("form", {"id": "exampleid"})
csrf = csrf.find('input', {'name': 'csrf'}).get("value")
print(csrf)

Gradirei qualsiasi tipo di aiuto, come si è realmente mi da fastidio. Vi ringrazio in anticipo!

beautifulsoup forms hidden-field python
2021-11-23 17:09:09
1

Migliore risposta

1

La selezione è ancora al lavoro, credo che ci sia un altro problema, forse non otterrete il codice html che vi aspettate.

Come alternativa per selezionare e ottenere il valore di nascosto <input> è possibile utilizzare i seguenti css selector:

soup.select_one('#exampleid input[name*="csrf"]')['value']

Esempio

from bs4 import BeautifulSoup

html = '''
<form id="exampleid" class="exampleclass" action="/ex/ex-ex/ex/2" method="post">
<div>
<input type="hidden" name="csrf" value="abcdefghijklmnopqrstuvwxyz">
</div></form>'''

soup = BeautifulSoup(html, "lxml")

csrf = soup.select_one('#exampleid input[name*="csrf"]')['value']

print(csrf)

Uscita

abcdefghijklmnopqrstuvwxyz
2021-11-24 07:51:04

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................