post 请求自动登陆github
Posted yijingjing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了post 请求自动登陆github相关的知识,希望对你有一定的参考价值。
‘‘‘访问登陆页面
请求URL:
https://github.com/login
请求方式:
GET
请求头:
COOKIES
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/75.0.3770.100 Safari/537.36
2.解析并提取token字符串
#正则
<input type="hidden" name="authenticity_token" value="(.*?)" />
#字符串
uj+bTJF3L3lfBg4YfijNJ0VPUqR0UWCmD0CPDGJY8jjT4GpzV6AYNx5VRFWzCxnpOdk3n62J9/qzeXbQEwXTCw==
‘‘‘
import requests
import re
login_url = ‘https://github.com/login‘
# login页面的请求头信息
login_header =
‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36‘
login_res = requests.get(url=login_url, headers=login_header)
# print(login_res.text)
# 解析提取token字符串
authenticity_token = re.findall(
‘<input type="hidden" name="authenticity_token" value="(.*?)" />‘,
login_res.text,
re.S
)[0]
print(authenticity_token)
login_cookies = login_res.cookies.get_dict()
# 2.开始登陆github
‘‘‘post请求自动登陆github:
请求URL:https://github.com/session
请求方式:POST
请求头:
Cookie
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36
请求体:
commit: Sign in
utf8: ?
authenticity_token: 0cfliMWH1jCxz8hDcBHBRSmN7sg6/VYvEAwnCdXUErfZPIfu52UGnh/S8mYguH1GAyGmBTuilMTXB6tL9Zsgzg==
login: *****
password: ******
webauthn-support: supported
‘‘‘
#session登陆url
session_url = ‘https://github.com/session‘
#请求头信息
session_headers =
‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36‘
#请求体信息
form_data =
"commit": "Sign in",
"utf8": "?",
"authenticity_token": authenticity_token,
"login": "****** ",
"password":"******",
"webauthn - support": "supported"
session_res = requests.post(
url=session_url,
headers=session_headers,
cookies=login_cookies,
data=form_data
)
with open(‘github.html‘,‘w‘,encoding=‘utf-8‘)as f:
f.write(session_res.text)
以上是关于post 请求自动登陆github的主要内容,如果未能解决你的问题,请参考以下文章
接口自动化,sso单点登陆不支持通过接口返回token,其他请求又需要token。解决方式,使用UI自动化的selenium框架登陆获取token