使用Python请求登录到Duolingo

2024-10-05 15:24:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我想登陆Duolingo个人资料的主页(学习),但我在使用Python请求使用我的凭据登录网站时遇到了一些问题。 我试着提出我理解的请求,但我在这方面几乎是个傻瓜,所以到目前为止一切都白费了。 非常感谢您的帮助

这是我用我自己的方式尝试的顺便说一下:

#The Dictionary Keys/Values and the Post Request URL were taken from the Network Source code in Inspect on Google Chrome

import requests

headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/81.0.4044.138 Safari/537.36'
}

login_data = 
{
'identifier': 'something@email.com',
'password': 'myPassword'
}

with requests.Session() as s:
    url = "https://www.duolingo.com/2017-06-30/login?fields="
    s.post(url, headers = headers, params = login_data)
    r = s.get("https://www.duolingo.com/learn")
    print(r.content)

post请求接收以下内容:

b'{"details": "Malformed JSON: No JSON object could be decoded", "error": "BAD_REQUEST_SCHEMA"}'

由于登录失败,学习页面的get请求将收到:

b'<html>\n <head>\n  <title>401 Unauthorized</title>\n </head>\n <body>\n  <h1>401
Unauthorized</h1>\n  This server could not verify that you are authorized to access the document you
requested.  Either you supplied the wrong credentials (e.g., bad password), or your browser does not
understand how to supply the credentials required.<br/><br/>\n\n\n\n </body>\n</html>'

对不起,如果我犯了愚蠢的错误。我对这一切了解不多。谢谢


Tags: thehttpscomyouurldatagetwww
1条回答
网友
1楼 · 发布于 2024-10-05 15:24:06

如果仔细检查POST请求,您可以看到:

  • 接受的内容类型为application/json
  • 字段比您提供的多(distinctIdlandingUrl
  • 数据作为json请求主体而不是url参数发送

您需要解决的唯一问题是如何获取distinctId,然后您可以执行以下操作:

编辑:

将电子邮件/密码作为json正文发送似乎就足够了,不需要获取distinctId,例如:

import requests
import json

headers = {'content-type': 'application/json'}

data = {
    'identifier': 'something@email.com',
    'password': 'myPassword',
    }

with requests.Session() as s:
    url = "https://www.duolingo.com/2017-06-30/login?fields="
    # use json.dumps to convert dict to serialized json string
    s.post(url, headers=headers, data=json.dumps(data))
    r = s.get("https://www.duolingo.com/learn")
    print(r.content)

相关问题 更多 >