def download(source_url):
try:
socket.setdefaulttimeout(20)
agents = ['Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1041.0 Safari/535.21','Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20120403211507 Firefox/12.0']
ree = urllib2.Request(source_url)
ree.add_header('User-Agent',random.choice(agents))
resp = urllib2.urlopen(ree)
htmlSource = resp.read()
return htmlSource
except Exception, e:
print e
return ""
I wrote this download function. How do I make it work with 301/302?
Ex: http://tumblr.com/tagged/long-reads my function doesn't work with this url.