WeInThis
WeInThis

Reputation: 547

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 enter image description here

and what I have written in the code is

token = soup.find('input', {'name':'_AntiCsrfToken'})['value'])
print(token)

but it gives me a error saying

    Traceback (most recent call last):
  File "C:\Users\HelloWorld.py", line 67, in <module>
    print(soup.find('input', {'name':'_AntiCsrfToken'})['value'])
  File "C:\Python\lib\site-packages\bs4\element.py", line 1292, in find
    l = self.find_all(name, attrs, recursive, text, 1, **kwargs)
AttributeError: 'str' object has no attribute 'find_all'

I quite dont understand if I have done it right or not. I do think I did it right but maybe I need to find it before from form-id than just go into hidden directly ?

Upvotes: 0

Views: 12631

Answers (2)

Luke
Luke

Reputation: 774

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

Upvotes: 0

johnashu
johnashu

Reputation: 2211

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>

<form action="process">
<input type="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

Upvotes: 2

Related Questions