gpt4 book ai didi

python - 如何使用请求模块中的 POST 登录 Github?

转载 作者:太空宇宙 更新时间:2023-11-04 00:47:55 25 4
gpt4 key购买 nike

我尝试使用以下代码登录 GitHub:

url = 'https://github.com/login'

headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
'login':'username',
'password':'password',
'authenticity_token':'Token that keeps changing',
'commit':'Sign in',
'utf8':'%E2%9C%93'
}

res = requests.post(url)
print(res.text)

现在,res.text 打印登录页面的代码。我知道这可能是因为 token 不断变化。我还尝试将 URL 设置为 https://github.com/session 但这也不起作用。

谁能告诉我生成 token 的方法。我正在寻找一种无需使用 API 即可登录的方法。我问过another question我提到我无法登录的地方。一条评论说我做得不对,可以在没有 Github API 帮助的情况下仅使用 requests 模块登录。

我:

So, can I log in to Facebook or Github using the POST method? I have tried that and it did not work.

用户:

Well, presumably you did something wrong

谁能告诉我我做错了什么?

在关于使用 session 的建议之后,我更新了我的代码:

s = requests.Session()
headers = {Same as above}

s.put('https://github.com/session', headers=headers)
r = s.get('https://github.com/')

print(r.text)

我仍然无法通过登录页面。

最佳答案

我认为您回到登录页面是因为您被重定向了,并且由于您的代码没有发回您的 cookie,所以您无法进行 session 。

您正在寻找 session 持久性,requests 提供了它:

Session Objects The Session object allows you to persist certain parameters across requests. It also persists cookies across all requests made from the Session instance, and will use urllib3's connection pooling. So if you're making several requests to the same host, the underlying TCP connection will be reused, which can result in a significant performance increase (see HTTP persistent connection).

s = requests.Session()

s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
r = s.get('http://httpbin.org/cookies')

print(r.text)
# '{"cookies": {"sessioncookie": "123456789"}}'

http://docs.python-requests.org/en/master/user/advanced/

关于python - 如何使用请求模块中的 POST 登录 Github?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38537963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com