Python Beautiful Soup-获取输入值 [英] Python Beautiful Soup - Getting input value
问题描述
我的计划是能够使用Bs4来获取_AntiCsrfToken.
My plan is to be able to grab the _AntiCsrfToken by using Bs4.
我有这个HTML,我的HTML来自
I have this HTML where my HTML comes from
我在代码中写的是
token = soup.find('input', {'name':'_AntiCsrfToken'})['value'])
print(token)
但这给我一个错误提示
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'
我完全不知道我是否做对了.我确实认为我做对了,但也许我需要从form-id之前找到它,而不是直接隐藏它?
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 ?
推荐答案
我不确定错误在哪里,但是我制作了一个html文件并将其放在我的服务器上,复制和粘贴您的文件都没有问题代码.
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..
唯一值得注意的区别(如果您还没有这样做的话)是我使用请求将html解析为BS4
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)
返回:
5435434354353453545
您也不需要
{'name':'_AntiCsrfToken'}
如此:
token = soup.find('input')['value']
会工作
这篇关于Python Beautiful Soup-获取输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!