Reptile POST request automatic login

# Get token random string
'' ' 
1. access the login page random string token obtaining
request the URL:
http://github.com/login
request method:
the GET
request header:
COOKIES
the User-- Agent: the Mozilla / 5.0 (the Windows NT 10.0; the WOW64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 63.0.3239.132 Safari / 537.36
2. parse and extract the token string
# regular
<the INPUT of the type = "hidden" name = "authenticity_token" value = "(. *?)" />
'' '
import requests
import  re
login_url='https://github.com/login'
Request header #login page
login_header={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
login_res=requests.get(url=login_url,headers=login_header)
#print(login_res.text)
# Parse extracted token string
authenticity_token=re.findall(
    '<input type="hidden" name="authenticity_token" value="(.*?)" />',
    login_res.text,
    re.S
)[0]
print(authenticity_token)
# Cookies to obtain information login page
#print(login_res.cookies)
login_cookies=login_res.cookies.get_dict()
# 2. Began landing github
session_url='http://github.com/session'
# Request header
session_headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
'' ' 
The POST request automatic login github:
request the URL:
http://github.com/session
request method:
POST
request header:
Cookie
User-Agent
request body:
the commit: Sign in
UTF8: ✓
authenticity_token: FLmfwPWcFB / gy50Y3lUfZduoWrRtTF9eWiAYWKwjlY9M4nFs9SvvqX3qQ / n1wJiaI5Blp1lHT7qe9m / == kofrgcw
Login:
password:
webauthn-Support: Unsupported
'' '

#-Information request
form_data={
          "commit":"Sign in",
          "utf8":"",
          "authenticity_token":authenticity_token,
          "login":"lzc01021033",
          "password":"lzc199901021033",
          "webauthn-support":"unsupported"
}
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)

 

Guess you like

Origin www.cnblogs.com/changgeyimeng/p/11115897.html