简体   繁体   English

Python 请求。 模拟 cookie

[英]Python requests. Emulating cookies

Im trying to scrape the web, but as a output getting an error with AADSTS165000: Invalid Request .我试图抓取网页,但作为输出, AADSTS165000: Invalid Request出现错误。

My Python code is pretty simple:我的 Python 代码非常简单:

import requests
s = requests.session()
link = 'https://login.microsoftonline.com/.../login'
email = {'loginfmt':'...',
             'passwd':'...',}
loging = s.post(link, data = email)

f = open('123.txt', 'w')
f.write(loging.text)
f.close()

But getting html output wiht The request did not return all of the form fields. Failure Reasons:[Missing session context cookie;Token was not provided;]但是获取带有The request did not return all of the form fields. Failure Reasons:[Missing session context cookie;Token was not provided;] html 输出The request did not return all of the form fields. Failure Reasons:[Missing session context cookie;Token was not provided;] The request did not return all of the form fields. Failure Reasons:[Missing session context cookie;Token was not provided;]

How can I emulate all the requirements to sucessefully loggin in and get the html?如何模拟成功登录并获取 html 的所有要求?

To inject cookies into your request, you need to have the cookies ready,要将 cookie 注入您的请求,您需要准备好 cookie,

assuming you have that already,假设你已经有了,

cookies = {"key":"value"}
loging = s.post(link, data = email, cookies=cookies)

You probably need to grab the session cookies after logging in, and send it with your next request.您可能需要在登录后获取会话 cookie,并将其与您的下一个请求一起发送。

So do也一样

cookies = s.cookies.get_dict()
loging = s.post(link, data = email, cookies=cookies)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM