how to use python to login page which requires session id responded by server on first request?

房东的猫 提交于 2019-12-21 04:06:54

问题


I am writing a script to log in to some webpage. I using request and request.session module for this purpose.On first request with login parameters server responses a session id.How to set that session id for further login to 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 above code, server responses sessionid on first request.How to use that sessionid on second request?


回答1:


You are already using requests.session(); it handles cookies for you, provided you keep using the session for all your requests:

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)

You probably do first need to use GET to get the session id set before posting to the login form.

The SESSIONID cookie is handled transparently for you.




回答2:


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)


来源:https://stackoverflow.com/questions/22300275/how-to-use-python-to-login-page-which-requires-session-id-responded-by-server-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!