url:https://github.com/sessionhtml
請求方式:postgit
請求頭:'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'github
請求體:web
commit: Sign incookie
utf8: ✓session
authenticity_token:post
ZVmXUBJA/KD5HBTsezlAPy/TwSivooXI2G6yiRkTjlZv2BvlYd8FL4XCVGIXpijMMN00/2Xf7SWc2iuqo0DzSA==url
login: ******code
password: ******orm
webauthn-support: supported
請求方式:get
請求頭:
name="authenticity_token" value="K+NjPJaMzAs07qMDF1WumVvAwUE0wDYobB4izS7fJMEhYu+J5RM1hEgw4417ysZqRM40lv69XsUoqrvulIxZ3w==
代碼實現
import requests import re login_url='https://github.com/login' #login頁面的請求頭 login_header={ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36' } login_res=requests.get(url=login_url,headers=login_header) print(login_res) #解析提取token authenticiyt_token = re.findall( 'input type="hidden" name="authenticity_token" value="(.*?)" />', login_res.text, re.S )[0] print(authenticiyt_token)
# session登陸url session_url = 'https://github.com/session' # 請求頭信息 session_headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36' } # 請求體信息 form_data = { "commit": "Sign in", "utf8": "✓", "authenticity_token": authenticity_token, "authenticity_token": "ZVmXUBJA/KD5HBTsezlAPy/TwSivooXI2G6yiRkTjlZv2BvlYd8FL4XCVGIXpijMMN00/2Xf7SWc2iuqo0DzSA==", "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)