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 ""
我写了这个下载功能。如何使其与 301/302 一起使用?
例如:http ://tumblr.com/tagged/long-reads我的功能不适用于此网址。