【问题标题】:Cookies must be enabled in your browser [Python Requests]必须在您的浏览器中启用 Cookie [Python 请求]
【发布时间】:2016-06-01 16:27:51
【问题描述】:

所以我尝试通过 python 登录我的 hotmail 帐户,并在我提出此请求时不断在页面上收到此响应

r = requests.post('https://login.live.com', auth=('Email', 'Pass'),verify=False)

Cookies must be allowed

Your browser is currently set to block cookies. Your browser must allow cookies before you can use a Microsoft account.

Cookies are small text files stored on your computer that tell Microsoft sites and services when you're signed in. To learn how to allow cookies, see online help in your web browser.

我还想提一下,我正在尝试 httpPOST 到此网页,因为我宁愿处理响应中的 cookie 并访问我的 microsoft 个人资料的其他页面(而不是仅仅通过 smtp 服务器访问我的电子邮件)

谢谢!

编辑:

import requests

s = requests.Session()
r = s.get('https://login.live.com',verify=False)
r = s.post('https://login.live.com', auth=('user', 'pass'),verify=False)
print r.status_code
print r.text

【问题讨论】:

    标签: python cookies python-requests


    【解决方案1】:

    使用requests.Session 保持会话(包括cookie):

    import requests
    
    s = requests.Session()
    res = s.get('https://login.live.com')
    cookies = dict(res.cookies)
    res = s.post('https://login.live.com', 
        auth=('Email', 'Password'),
        verify=False, 
        cookies=cookies)
    

    【讨论】:

    • 感谢您的回复!超级奇怪,因为我打印了帖子的文本,它说必须启用 cookie(相同的消息)。但它确实在我的 cookie jar 中给了我一个 cookie,非常奇怪我会继续尝试会话,但谢谢!
    • 大概你必须先调用s.get(),让服务器有机会设置cookie。
    • 嗯,我明白你的意思。尽管我更新了我当前在问题底部使用的代码,但仍然很奇怪。很抱歉在这个大声笑上表现得如此糟糕,以前从未出现过这个错误。
    • @k9b,在您的帖子消息中使用cookies 参数,即:s.post('https://login.live.com', auth=('Email', 'Pass'),verify=False, cookies={'my': 'cookies'})
    • 我实际上并不认为我做错了什么。我只是不认为微软希望我通过 http 请求登录,它只是给我这个作为默认错误大声笑。感谢您的建议
    猜你喜欢
    • 2019-05-23
    • 1970-01-01
    • 2023-01-20
    • 2015-05-19
    • 2017-11-15
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多