使用Python登录到Github,必须启用Cookies才能使用GitHub [英] Loggin in to Github with Python, Cookies must be enabled to use GitHub
本文介绍了使用Python登录到Github,必须启用Cookies才能使用GitHub的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试登录Python,我知道他们有一个API,但这更多的是为了使用Python建立我的学习技能,我最终可能会采用API的方式,但是无论如何,我都试图登录github,并且我的脚本出现错误/响应:
I am trying to login to Python, I know they have an API, but this is more to build my learning skills with Python, I may eventually go the API way, but anyway, I am trying to login to github, and with my script I am getting the error/response:
Cookies must be enabled to use GitHub.
这意味着我必须启用cookie,对吗?但是我敢肯定我有..这是我的代码
Which means I must enable cookies,right? But I am sure I have.. Here is my code
def github_login():
# get the auth token, and sign in button value to sen with the params
git = bs(requests.get('https://github.com/login').text, 'html.parser')
auth_token = git.find("input", {"name": "authenticity_token"}).attrs['value']
commit = git.find("input", {"name": "commit"}).attrs['value']
#Cookies must be enabled to use GitHub
session = requests.session()
#send the data, with auth tokenn and button value
data = {
'username': username,
'password': password,
'commit' : commit,
'authenticity_token' : auth_token
}
headers = {
"Host": "www.github.com",
"Origin": "https://www.github.com",
"X-Requested-With": "XMLHttpRequest",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5",
}
login = session.post('https://github.com/session', data=data)
print login.text
if login.status_code == 301:
logged_in = True
return True
return False
如何解决此错误?
推荐答案
同一cookie必须在请求中持久存在,因此请确保您的GET和POST请求都在同一会话中进行:
The same cookie must persist across requests, so ensure your GET and POST requests are both made within the same session:
def github_login():
s = requests.Session()
# get the auth token, and sign in button value to sen with the params
soup = bs(s.get('https://github.com/login').text, 'html.parser')
Aaaand:
login = s.post('https://github.com/session', data=data)
这篇关于使用Python登录到Github,必须启用Cookies才能使用GitHub的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文