Search code examples
pythonurllib2urllibcookielib

urllib & cookielib - downloading images


this is what i got so far:

import urllib, urllib2, cookielib

jar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
login_data = urllib.urlencode({'User' : 'c1300', 'Pass' : '5800009'})
opener.open('http://www.sis.com/default.asp', login_data)

i got this code from somewhere around stack overflow as well. what i don't know how to do is how to download an image using the cookie processor (as in downloading an image only accessible when you're logged in) any help appreciated, thanks in advance


Solution

  • You're going to need to obtain a cookie from the site by logging in, which means interacting with the login form, storing the cookie you get, and sending it back to the webserver when you attempt to get the image. Instead, consider Mechanize: http://wwwsearch.sourceforge.net/mechanize/. This library abstracts out much of the pain of what you are trying to do.