Piers Thomas
Piers Thomas

Reputation: 327

Python - BeautifulSoup pulling values value from input

I am trying to extract the CSRFToken from a webpage. My code is below:

from bs4 import BeautifulSoup
import requests

r = requests.get('https://www.clos19.com/en-gb/login')
soup = BeautifulSoup(r.text, 'html.parser')
print(soup.find_all('input', type='hidden'))

The response from the terminal is below:

[<input name="CSRFToken" type="hidden" value="790bf524-642c-4679-a036-8f126fe14940"/>]

How do I only get the value 790bf524-642c-4679-a036-8f126fe14940 to be outputted?

Upvotes: 0

Views: 1631

Answers (2)

Arnav Chawla
Arnav Chawla

Reputation: 556

Here is a simple solution:

from bs4 import BeautifulSoup
import requests

r = requests.get('https://www.clos19.com/en-gb/login')
soup = BeautifulSoup(r.text, 'html.parser')
print(soup.find('input', type='hidden')["value"])

By adding ["value"], the printed output is only the csrf token.

Upvotes: 1

JON
JON

Reputation: 1738

for value in soup.find_all('input', type='hidden'):
    print(value.get('value'))

or

print(soup.find('input', type='hidden').get('value'))

Upvotes: 1

Related Questions