使用python请求登录

时间:2018-03-11 20:43:15

标签: python curl login python-requests

我想使用Python的requests模块登录网页https://www.tennistv.com/login

我在this帖子上看过如何实现这一点,但是,我被卡住了。有效载荷如下所示:

payload = {"Email":"[MAIL]","Password":"[PASSWORD]"}

然后我按如下方式开始会话:

with requests.Session() as s:
    p = s.post('https://www.tennistv.com/api/users/v1/login', data=payload, headers=headers)
    # print the html returned or something more intelligent to see if it's a successful login page.
    print(p.text)

这会产生以下错误:

{
    "error": {
        "validationErrors": [{
            "key": "loginModel",
            "value": [""]
        }],
        "errorMessage": "There was an error while validating input data (UR001)",
        "errorCode": "UR001",
        "userErrorCode": "UR001"
    }
}

即使我将来自Chrome开发者控制台的请求作为cURL命令复制,然后执行cURL命令,我也会收到同样的错误。

cURL命令如下所示:

curl "https://www.tennistv.com/api/users/v1/login" -H "Pragma: no-cache" -H "Origin: https://www.tennistv.com" -H "Accept-Encoding: gzip, deflate, br" -H "Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36" -H "content-type: application/json" -H "Accept: */*" -H "Cache-Control: no-cache" -H "Referer: https://www.tennistv.com/login" -H "Connection: keep-alive" --data-binary "^{^\^"Email^\^":^\^"[MAIL]^\^",^\^"Password^\^":^\^"[PASS]^\^"^}" --compressed

我做错了什么? cURL命令与浏览器发送的请求之间的区别在哪里?

1 个答案:

答案 0 :(得分:0)

我可以解决这个问题。有效载荷必须如下(不同的引号):

payload = {'Email':'[MAIL]','Password':'[PASSWORD]'}