02post请求自动登陆github
post请求自动登陆github
url:https://github.com/session
请求方式:post
请求头:'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:
ZVmXUBJA/KD5HBTsezlAPy/TwSivooXI2G6yiRkTjlZv2BvlYd8FL4XCVGIXpijMMN00/2Xf7SWc2iuqo0DzSA==
-
login: ******
-
password: ******
-
webauthn-support: supported
1.获取token随机字符串
请求方式:get
请求头:
name="authenticity_token" value="K+NjPJaMzAs07qMDF1WumVvAwUE0wDYobB4izS7fJMEhYu+J5RM1hEgw4417ysZqRM40lv69XsUoqrvulIxZ3w==
代码实现
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)
#解析提取token
authenticiyt_token = re.findall(
'input type="hidden" name="authenticity_token" value="(.*?)" />',
login_res.text,
re.S
)[0]
print(authenticiyt_token)
2.开始登陆Github
# session登录url
session_url = 'https://github.com/session'
# 请求头信息
session_headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
}
# 请求体信息
form_data = {
"commit": "Sign in",
"utf8": "✓",
"authenticity_token": authenticity_token,
"authenticity_token": "ZVmXUBJA/KD5HBTsezlAPy/TwSivooXI2G6yiRkTjlZv2BvlYd8FL4XCVGIXpijMMN00/2Xf7SWc2iuqo0DzSA==",
"login": "*****",
"password": "*****",
"webauthn-support": "supported"
}
session_res = requests.post(url=session_url,
headers=session_headers,
cookies=login_cookies,
data=form_data)
with open('github3.html', 'w', encoding='utf-8') as f:
f.write(session_res.text)