Home > Net >  Python - not able to extract value of hidden input
Python - not able to extract value of hidden input

Time:11-25

I am trying to extract the value of a hidden input tag. Even though the element exists in the HTML i can't find it with bs4.

This is the error message i get:

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

This is the html on the webpage:

<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>

And this is my current code:

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

I would appreciate any kind of help as it is really bothering me. Thank you in advance!

CodePudding user response:

Your selection is still working, think there is another issue, maybe you wont get the html you expect.

As alternativ to select and get the value of this hidden <input> you can use the following css selector:

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

Example

from bs4 import BeautifulSoup

html = '''
<form id="exampleid"  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)

Output

abcdefghijklmnopqrstuvwxyz
  • Related