day01 post自动请求登录github
''' POST请求自动登录github: 请求URL: https://github.com/session 请求方式: POST 请求头: Cookie User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36 请求体: commit: 登入 utf8: ✓ authenticity_token: /daqhg+R7XHv6Xef7qNLpxKlRB5qNUVuqU2tWqb36gxAg9aaGxg6xywnLji5bfbu76p/XnCGuLs3AFLf2lK+/Q== login:*** password:*** webauthn-support: supported ''' # 1.获取token随机字符串 ''' 1.访问登录页面获取token随机字符串 请求URL: https://github.com/login 请求方式: GET 请求头: COOKIES User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36 2.解析并提取token字符串 #正则 <input type="hidden" name="authenticity_token" value="(.*?)"/> ''' import requests import re login_url = 'https://github.com/login' # login 页面的请求头信息 login_header = { 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 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() #2.开始登录github ''' POST请求自动登录github: 请求URL: https://github.com/session 请求方式: POST 请求头: Cookie User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36 请求体: commit: 登入 utf8: ✓ authenticity_token: /daqhg+R7XHv6Xef7qNLpxKlRB5qNUVuqU2tWqb36gxAg9aaGxg6xywnLji5bfbu76p/XnCGuLs3AFLf2lK+/Q== login:*** password:*** webauthn-support: supported ''' # session登录url session_url = 'https://github.com/session' # 请求头信息 session_headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 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('github3.html','w',encoding='utf-8')as f: f.write(session_res.text)