使用Beautifulsoup在html页面中找到CSRF令牌 [英] finding the CSRF token inside an html page using Beautifulsoup
本文介绍了使用Beautifulsoup在html页面中找到CSRF令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HTML看起来像这样
The HTML looks something like this
<input type="hidden" name="csrfToken" value="ajax:SOME_TOKEN"/>
我已经尝试了几种不同的方法,但是我一直遇到错误.我认为这种方式看起来正确,但显然不正确.
I've tried this a few different ways, but I keep getting an error. I thought this way looked right, but apparently not.
soup = BeautifulSoup(html_page)
soup.find('input', {'name':'csrfToken'})
我不断得到:
TypeError: 'expected string or buffer'
有什么想法吗?
推荐答案
这是从给定输入中提取CSRF令牌的一种方法:
This is one way of extracting the CSRF token from the given input:
from bs4 import BeautifulSoup
html = '<input type="hidden" name="csrfToken" value="ajax:SOME_TOKEN"/>'
soup = BeautifulSoup(html)
token = soup.find('input', {'name':'csrfToken'})['value']
print token
运行此命令的结果是:
ajax:SOME_TOKEN
通过查看您的示例,实际提取html元素似乎是正确的.可能是您未设置html_page
或其他类型的类型(即不是字符串)吗?
By looking at your example, the actual extraction of the html element seems correct. Can it be that your html_page
is not set or some other kind of type (i.e. not a string) ?
这篇关于使用Beautifulsoup在html页面中找到CSRF令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文