''' POST請求自動登錄ithub 請求URL: https://github.com/login 請求方式: POST 請求頭: Cookie User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36 請求體: utf8: ✓ authenticity_token: NVuVfOGJn7mujgE0t+rglSqXY8hpYaIjA4PpO2Vd+g9oTD0zdUAEEKQYjWaWX53w3olyb9H6RvJ8eKVLj8hSBQ== login: zxr**** password: zxr***** webauthn-support: supported commit: Sign in w ''' # 1.獲取token隨機字符串 ''' 1.訪問登錄頁面獲取token隨機字符串 請求URL: https://github.com/login 請求方式: GET 請求頭: COOKIES User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 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 10.0; Win64; x64) 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.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
''' 請求URL: https://github.com/login 請求方式: POST 請求頭: Cookie User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36 請求體: utf8: ✓ authenticity_token: NVuVfOGJn7mujgE0t+rglSqXY8hpYaIjA4PpO2Vd+g9oTD0zdUAEEKQYjWaWX53w3olyb9H6RvJ8eKVLj8hSBQ== login: zxr*** password:********
commit: Sign in webauthn-support: supported '''
# session登陸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/70.0.3538.25 Safari/537.36 Core/1.70.3704.400 QQBrowser/10.4.3587.400'}
# 請求體信息 form_data = {"commit": "Sign in", "utf8": "✓", "authenticity_token": authenticity_token, "login": "zxr******", "password": "zxr*****", "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)