简体   繁体   English

Python-BeautifulSoup从输入中提取值

[英]Python - BeautifulSoup pulling values value from input

I am trying to extract the CSRFToken from a webpage. 我正在尝试从网页中提取CSRFToken。 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? 如何只输出值790bf524-642c-4679-a036-8f126fe14940?

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

or 要么

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

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. 通过添加[“ value”],打印的输出仅是csrf令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM