我正在尝试使用python 3使用requests和lxml登录网页。但是,在向登录页面发送帖子请求后,我无法输入登录后可用的页面。我错过了什么?
import requests
from lxml import html
session_requests = requests.session()
login_URL = 'https://www.voetbal.nl/inloggen'
r = session_requests.get(login_URL)
tree = html.fromstring(r.text)
form_build_id = list(set(tree.xpath("//input[@name='form_build_id']/@value")))[0]
payload = {
'email':'mom.soccer@mail.com',
'password':'testaccount',
'form_build_id':form_build_id
}
headers = {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding':'gzip, deflate, br',
'Accept-Language':'nl-NL,nl;q=0.9,en-US;q=0.8,en;q=0.7',
'Cache-Control':'max-age=0',
'Connection':'keep-alive',
'Content-Type':'multipart/form-data; boundary=----WebKitFormBoundarymGk1EraI6yqTHktz',
'Host':'www.voetbal.nl',
'Origin':'https://www.voetbal.nl',
'Referer':'https://www.voetbal.nl/inloggen',
'Upgrade-Insecure-Requests':'1',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
result = session_requests.post(
login_URL,
data = payload,
headers = headers
)
pvc_url = 'https://www.voetbal.nl/club/BBCB10Z/overzicht'
result_pvc = session_requests.get(
pvc_url,
headers = headers
)
print(result_pvc.text)
此示例中的帐户已激活,但它只是我创建的一个测试帐户,可以在此处提出我的问题。随意尝试一下。
答案 0 :(得分:1)
答案:
那里存在多个问题:
有效负载:' form_id':' voetbal_login_login_form'失踪。谢谢@ t.m.adam
Cookie:请求丢失的Cookie。它们似乎是静态的,所以我尝试手动添加它们,这是有效的。谢谢@match和@Patrick Doyle
标题:删除了'内容类型'线;其中包含一个动态部分。
登录现在就像魅力一样!