python reptile Day1 (Post request automatic landing github)

'' ' 
The POST request automatic landing ithub 

    request the URL: 
    https://github.com/login 

    request method: 
        the POST 

    request header: 
        cookies 
        the User-- Agent: the Mozilla / 5.0 (the Windows NT 10.0; Win64; x64-) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 75.0.3770.100 Safari / 537.36 

    request body: 
        UTF8: ✓ 
        authenticity_token: NVuVfOGJn7mujgE0t rglSqXY8hpYaIjA4PpO2Vd + == + g9oTD0zdUAEEKQYjWaWX53w3olyb9H6RvJ8eKVLj8hSBQ 
        Login: zxr997947 
        password: zxr316794 
        webauthn-Support: Supported 
        the commit: in Sign 
        W 


    '' '
 
#. 1 acquires a random token string 
'' ' 
1. visit the landing page for a token random string 
    request URL:
           https://github.com/login 
    request method: 
           the GET 
    request header: 
          COOKIES 
          the User-- Agent: the Mozilla / 5.0 (the Windows NT 10.0; Win64; x64-) AppleWebKit / 537.36 (KHTML, like the Gecko) the Chrome / 75.0.3770.100 Safari / 537.36 
2. parse the token and extracts the string 
# n-side 
<INPUT type = "hidden" name = "authenticity_ .token" value = "(*.?)" /> 
'' ' 
Import Requests 
Import Re 
LOGIN_URL =' HTTPS: // github.com/login ' 

# Login page request header 
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)

# Parse extracted token string 
authenticity_token = re.findall ( '<the INPUT of the type = "hidden" name = "authenticity_token" value = "(. *?)" />',login_res.text,re.S)[0] 
Print (authenticity_token) 

# cookies to obtain information login page 
# Print (of the type (login_res.cookies)) 
# Print (of the type (login_ RES. cookies.get_ dict ())) 
login_cookies = login_res.cookies.get_dict () 

# 2. began landing GitHub '' ' 
request the URL: 
    https://github.com/login 
    request method: 
        the POST 
    request header: 
        cookies 
        the User-- Agent: the Mozilla / 5.0 (the Windows NT 10.0; Win64; x64-) AppleWebKit / 537.36 (KHTML, like the Gecko) the Chrome /75.0.3770.100 Safari / 537.36 
    request body:



 
        UTF8: ✓
        authenticity_token: NVuVfOGJn7mujgE0t+rglSqXY8hpYaIjA4PpO2Vd+g9oTD0zdUAEEKQYjWaWX53w3olyb9H6RvJ8eKVLj8hSBQ==
        login: zxr997947
        password: zxr316794
        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": "zxr997947",
"password": "zxr316794",
"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)

  

Guess you like

Origin www.cnblogs.com/Auraro997/p/11115124.html