Using requests module, how to handle 'set-cookie' in request response?

后端 未结 3 967
没有蜡笔的小新
没有蜡笔的小新 2020-12-24 03:14

I\'m attempting to open a login page (GET), fetch the cookies provided by the webserver, then submit a username and password pair to log into the site (POST).

Look

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-24 03:33

    There's an included class called a session which automatically handles this sort of thing for you. You can create an instance of it, and then call get and set right on that instance instead.

    import requests
    
    URL1 = 'login prompt page'
    URL2 = 'login submission URL'
    
    session = requests.Session()
    
    r = session.get(URL1)
    r2 = session.post(URL2, data="username and password data payload")
    

提交回复
热议问题