Skip to content Skip to sidebar Skip to footer

Python Beautiful Soup - Getting Input Value

My plan is to be able to grab the _AntiCsrfToken by using Bs4. I have this HTML where my HTML comes from and what I have written in the code is token = soup.find('input', {'name

Solution 1:

I am not sure where the error lies for you but I have made a little html file and put it on my server and I have no problem copying and pasting your code..

The only noticeable difference (if you have not done it) is that I am using requests to parse the html to BS4

I think maybe it is a parsing problem.

HTML

<html><formaction="process"><inputtype="hidden"name="_AntiCsrfToken"value="5435434354353453545"></form></html>

Python:

from bs4 import BeautifulSoup as bs4
import requests

r = requests.get('http://maffaz.com/so.html')
html_bytes = r.text
soup = bs4(html_bytes, 'lxml')
token = soup.find('input', {'name':'_AntiCsrfToken'})['value']
print(token)

returns:

5435434354353453545

Also you do not need

{'name':'_AntiCsrfToken'}

so:

token = soup.find('input')['value']

Will work

Solution 2:

Maybe try using CSS selectors?

from bs4 import BeautifulSoup

html = """
<html>
<input type="hidden" name="_AntiCsrfToken" value="5435434354353453545">
</html>
"""

soup = BeautifulSoup(html, 'lxml')
csrf = soup.select_one('input[name=_AntiCsrfToken]')['value']
print(csrf)

Output: 5435434354353453545

Post a Comment for "Python Beautiful Soup - Getting Input Value"