Maybe using cookielib.CookieJar can help you. For example, when sending to a page containing a form:
import urllib2 import urllib from cookielib import CookieJar cj = CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) # input-type values from the html form formdata = { "username" : username, "password": password, "form-id" : "1234" } data_encoded = urllib.urlencode(formdata) response = opener.open("https://page.com/login.php", data_encoded) content = response.read()
EDIT:
After Peter's comment, I thought a little. From the docs:
The CookieJar class stores HTTP cookies. It extracts cookies from HTTP requests and returns them in HTTP responses. Examples of using CookieJar, if necessary, the use of cookies automatically ends. Subclasses are also responsible for storing and retrieving cookies from a file or database.
This way, any requests you make with your CookieJar instance will be processed automatically. Kinda, how is your browser ::
I can only speak from my own experience, and my 99% use of cookies is to get a cookie and then send it with all subsequent requests in this session. The code above only handles this, and it does it transparently.
Morten Jensen Nov 21 2018-11-11T00: 00Z
source share