# 1, acquired random string token "," " 1, login screen to access random string token acquisition request url: request method: GET request header: Cookies the User_Agent: 2, parses and extracts the token string # regular <input type =" hidden "name =" authenticity_token "value =" (. *?) "/> " "" Import requests Import Re LOGIN_URL = ' https://github.com/login ' # request header login page login_header = { ' the User_Agent ' : ' the Mozilla / 5.0 (the Windows NT 10.0; the WOW64) AppleWebKit / 537.36 (KHTML,like Gecko) Chrome/73.0.3683.86 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页面的cookie信息 print(type(login_res.cookies)) print(type(login_res.cookies.get_dict())) login_cookies = login_res.cookies.get_dict() # 2、开始登陆github """" post request automatic login github: request URL: http://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 /73.0.3683.86 Safari / 537.36 request body: the commit: Sign in UTF8: ✓ authenticity_token: MBlkk / dibjj0nhvrHc02Q2hK5xP3gKg / TaaaAObXGYqsL4Hl8OubDPzrgF9cTWNNp6cxrd0WmmUl77yur0aY / A == Login: jiangbaoyabo password: ************ webauthn-Support: Supported "" " # the session login 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/73.0.3683.86 Safari/537.36' } # 请求体信息 form_data = { "commit": "Sign in", "utf8": "✓", "authenticity_token": authenticity_token, "login": ", "jiangbaoyabo"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)