Python TypeError,尝试分析令牌值,但不可能
我试图解析HTML页面中的一个令牌,在Hackthebox登录页面上创建一个autologin,但是脚本返回了一个错误,我现在不知道为什么。 要登录此页面,我需要“\u令牌”值 我尝试分析的登录页面:Python TypeError,尝试分析令牌值,但不可能,python,html,parsing,beautifulsoup,Python,Html,Parsing,Beautifulsoup,我试图解析HTML页面中的一个令牌,在Hackthebox登录页面上创建一个autologin,但是脚本返回了一个错误,我现在不知道为什么。 要登录此页面,我需要“\u令牌”值 我尝试分析的登录页面: <div class="col-xs-12 col-md-6 col-md-offset-3 col-lg-4 col-lg-offset-4"> <form id="loginForm" role="form" method="POST" ac
<div class="col-xs-12 col-md-6 col-md-offset-3 col-lg-4 col-lg-offset-4">
<form id="loginForm" role="form" method="POST" action="https://www.hackthebox.eu/login">
<input type="hidden" name="_token" value="yIfppPolJsSoJbKhbI0mtB487kT8qXgsqlbFAxXw">
<div class="form-group ">
<label class="control-label" for="email">E-Mail</label>
<input type="text" placeholder="" required="" value="" name="email" id="email" class="form-control"> </div>
<div class="form-group ">
<label class="control-label" for="password">Password</label>
<div id="passwordArea">
<input type="password" placeholder="" required="" value="" name="password" id="password" class="form-control">
</div>
</div>
此脚本的结果是:
Traceback (most recent call last):
File "gg.py", line 18, in <module>
token = html.body.find('input', attrs={'name':'_token','type':'hidden'})['value']
TypeError: 'NoneType' object has no attribute '__getitem__'
回溯(最近一次呼叫最后一次):
文件“gg.py”,第18行,在
token=html.body.find('input',attrs={'name':'u token','type':'hidden'})['value']
TypeError:“非类型”对象没有属性“\uuuu getitem\uuuu”
服务器在未找到用户代理标头时返回错误页。在指定了用户代理的get()
方法中设置headers=
参数就足够了:
import requests
from bs4 import BeautifulSoup
headers = {'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0'}
session = requests.Session()
response = session.get('https://www.hackthebox.eu/login', headers=headers)
html = BeautifulSoup(response.text, 'lxml')
token = html.body.find('input', attrs={'type':'hidden'})['value']
print(token)
这将打印您的令牌:
g1E39m2kn4sViJkoshbQQcZPS4HpWpf8Pxxme6jA
g1E39m2kn4sViJkoshbQQcZPS4HpWpf8Pxxme6jA