Keeping a session in python while making HTTP requests

前端 未结 3 809
南方客
南方客 2020-12-05 02:51

I need to write a python script that makes multiple HTTP requests to the same site. Unless I\'m wrong (and I may very well be) urllib reauthenticates for every request. Fo

3条回答
  •  抹茶落季
    2020-12-05 03:28

    Use Requests library. From http://docs.python-requests.org/en/latest/user/advanced/#session-objects :

    The Session object allows you to persist certain parameters across requests. It also persists cookies across all requests made from the Session instance.

    s = requests.session()
    
    s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
    r = s.get("http://httpbin.org/cookies")
    
    print r.text
    # '{"cookies": {"sessioncookie": "123456789"}}'
    

提交回复
热议问题