# 1 Gets tokrn random string '' ' 1. Access login page for a token random string request the URL of: https://github.com/login request method: GET request header: Cookies the User-Agent: Mozilla / 5.0 (Windows NT 10.0; Win64; x64-) AppleWebKit / 537.36 (KHTML, like the Gecko) the Chrome / 76.0.3809.36 Safari / 537.36 2. analytical and extracted token string # regular <input type = "hidden" name = "authenticity_token" value = "(. *?) "/> '' ' Import requests Import Re LOGIN_URL = ' https://github.com/login ' # Login page request header login_header = { 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.36 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信息 print(type(login_res.cookies)) print(type(login_res.cookies.get_dict())) login_cookies = Login_res.cookies.get_dict () # Start Log GitHub '' ' the POST request automatic login github: request URL: https://github.com/ request method: the POST request body: the commit: Sign in UTF8: ✓ authenticity_token: QTBo + c5aNH2UCCkmFeDe0o3fZpw9xOzfU0MffS2J + + + A3UUm4T9su2R2UqqqDROfKPE QKpAv2bb81u0dg lK0Ow == Login: cmd *** password: ********** webauthn-Support: Supported '' ' # the session login URL session_url = ' https://github.com/ the session ' # request header session_headers = { ' the User-- Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.36 Safari/537.36'} # 请求体信息 from_data = { "commit": "Sign in", "utf8": "✓", "authenticity_token": authenticity_token, "login": "cmd***", "password": "***********", "webauthn-support": "supported" } session_res = requests.post(url=session_url, headers=session_headers, cookies=login_cookies, data=from_data) with open('github.html','w',encoding='utf-8') as f: f.write(session_res.text)