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この URL では関数が機能しません。