我正在尝试使用请求和 lxml 使用 python 3 登录网页。但是,向登录页面发送post请求后,我无法进入登录后可用的页面。我错过了什么?
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)
此示例中的帐户已激活,但它只是我创建的一个测试帐户,用于在此处提出我的问题。欢迎试用。
最佳答案
回答:
存在多个问题:
有效载荷:“form_id”:“voetbal_login_login_form”丢失。谢谢@t.m.adam
Cookies:请求丢失的 cookies。它们似乎是静态的,所以我尝试手动添加它们,这很有效。感谢@match 和@Patrick Doyle
标题:删除了“内容类型”行;其中包含一个动态部分。
登录现在就像一个魅力!
关于python - 使用python请求登录网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48410064/