post request automatic landing github

'' 'Access to the landing page 
request the URL of:
https://github.com/login
request method:
GET
request header:
COOKIES
the User-Agent: Mozilla / 5.0 (Windows NT 10.0; WOW64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome Safari /75.0.3770.100 / 537.36
2. parse and extract the token string
# regular
<the INPUT of the type = "hidden" name = "authenticity_token" value = "(. *?)" />

# strings
uj + bTJF3L3lfBg4YfijNJ0VPUqR0UWCmD0CPDGJY8jjT4GpzV6AYNx5VRFWzCxnpOdk3n62J9 / qzeXbQEwXTCw ==
'' '
Import requests
Import Re

LOGIN_URL =' 'https://github.com/login
request header # login page
login_header = {
' - Agent-the User ':' the Mozilla / 5.0 (the Windows NT 10.0; the WOW64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 75.0.3770.100 Safari/537.36'

}
= requests.get login_res (URL = LOGIN_URL, headers = login_header)
# Print (login_res.text)
# direct extraction token string
authenticity_token the 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. began landing GitHub
' '' POST request automatically log github:
request URL : https: //github.com/session
request method: POST
request header:
cookies
the User-- Agent: the Mozilla / 5.0 (the Windows NT 10.0; the WOW64) AppleWebKit / 537.36 (KHTML, like the Gecko) the Chrome / 75.0.3770.100 Safari / 537.36
request body:
the 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)

Guess you like

Origin www.cnblogs.com/yijingjing/p/11115006.html