How to use python to log in, which requires a session identifier that the server responds on the first request?

I am writing a script to enter some web page. I use the request and request.session module for this. On the first request with the parameters of the login server, the session identifier is indicated. How to set this session identifier for further access to the same page.

url = "some url of login page"
payload = {'username': 'p05989', 'password': '123456'}
with requests.session() as s:
    s.post(url1, data=payload)
    sessionid = s.cookies.get('SESSIONID')
    print(sessionid)
    r = requests.get(url,data=payload)
    print(r.text)

in the above code, serverid replies sessionid to the first request. How to use this sessionid for the second request?

+4
source share
2 answers

requests.session(); cookie , , :

url = "some url of login page"
payload = {'username': 'p05989', 'password': '123456'}
with requests.session() as s:
    # fetch the login page
    s.get(url)

    # post to the login form
    r = s.post(url1, data=payload)
    print(r.text)

, GET, ​​ .

cookie SESSIONID .

+6
import requests
import webbrowser

url = "https://www.invezta.com/investorsignup.aspx"


payload = {'login-email':  'email',
    'login-pwd': 'password'}

with requests.session() as s:
    # fetch the login page
    s.get(url)

    url1='https://www.invezta.com/Pdf_creator.aspx?User_ID='

    # post to the login form
    r = s.post(url1, data=payload)
    print(r.text)
0

Source: https://habr.com/ru/post/1530969/


All Articles