Use Cookies in Python

I was trying to download tons of files from a website, so I wrote a python script to automate this procedure. However, the website requires to login. Thus, I have to enable my script to login automatically. This leads me to use cookies, which will maintain the session and keep it alive after I succeed in login.

from cookielib import CookieJar
cookie = CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
formdata = { "username" : <myusername>, "password": <mypassword>}
data_encoded = urllib.urlencode(formdata)
response ="http://<mywebsite>/user/login.html", data_encoded)
content =

Luckily, the website I am connecting to is not using any verification code, i.e. CAP.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s