尝试使用Python登录时,网站会抱怨已禁用的Cookie

时间:2017-07-27 17:55:27

标签: python python-requests

我正试图通过网站登录我的电子邮件帐户(不能使用IMAP)。我使用Python Requests模块。

我查看了Firefox在登录和复制数据时所做的POST请求。当我提交并查看回复的内容时,它会显示“要访问您的邮箱,您必须在浏览器中启用Cookie。”

我检查了Session对象的cookie,它有cookie。我做错了什么?

以下是我所拥有的,包含可用于测试登录的示例帐户数据。

import requests


user = "for_testing@mail.com"
psswd = "fortesting"
website = "https://www.mail.com"
POST_url = "https://login.mail.com/login#.1258-header-login1-2"
user_agent = "Mozilla/5.0 (Linux; Android 6.0.1; SM-G920V Build/MMB29K)"

# from the POST request info from Firefox's developer tools
params = {
    "service": "mailint",
    "uasServiceID": "mc_starter_mailcom",
    "successURL": "https://$(clientName)-$(dataCenter).mail.com/login",
    "loginFailedURL": "https://www.mail.com/int/logout/?ls=wd",
    "loginErrorURL": "https://www.mail.com/int/logout/?ls=te",
    "edition": "int",
    "lang": "en",
    "usertype": "standard",
    "username": "for_testing@mail.com",
    "password": "fortesting"
}

# these are the headers sent via Firefox upon a successful login
browser_headers = {
#    leaving this uncommented gives us an error page "request not understood"
#    "Host": "login.mail.com",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "en-US,en;q=0.5",
    "Accept-Encoding": "gzip, deflate, br",
    "Referer": "https://www.mail.com/int/",
    "Content-Type": "application/x-www-form-urlencoded",
    "Content-Length": "358",
#    not sending this, because the cookies are already set
#    "Cookie": "cookieKID=kid%40autoref%40mail.com; cookiePartner=kid%40autoref%40mail.com; ushallpass=true",
    "Connection": "keep-alive",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": user_agent
}

sess = requests.Session()
sess.get(website)
# setting this cookie manually
sess.cookies["ushallpass"] = "true"
print(sess.cookies)
a = sess.post(POST_url, headers=browser_headers, data=params)
# this shows that we got the error website and not the actual account page
print(a.text)

修改 即使尝试不手动添加“ushallpass”cookie,问题仍然存在。像这样:

sess = requests.Session()
sess.get(website)
a = sess.post(POST_url, headers=browser_headers, data=params)
# this shows that we got the error website and not the actual account page
print(a.text)

编辑2: 尝试传递未修改的cookie明确地获得第一个请求(我认为Session()对象自动执行)。

sess = requests.Session()
sess.get(website)
a = sess.post(POST_url, cookies=sess.cookies, data=params)
print(a.text)

0 个答案:

没有答案
相关问题