02post請求自動登錄github

post請求自動登錄github

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

  1. commit: Sign incookie

  2. utf8: ✓session

  3. authenticity_token:post

    ZVmXUBJA/KD5HBTsezlAPy/TwSivooXI2G6yiRkTjlZv2BvlYd8FL4XCVGIXpijMMN00/2Xf7SWc2iuqo0DzSA==url

  4. login: ******code

  5. password: ******orm

  6. webauthn-support: supported

1.獲取token隨機字符串

url:https://github.com/login

請求方式: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)

2.開始登錄Github

# 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)
相關文章
相關標籤/搜索