0
存在与gif图像的链接,但urllib2无法下载它。urllib2.urlopen无法获取图像,但浏览器可以
import urllib.request as urllib2
uri = 'http://ums.adtechjp.com/mapuser?providerid=1074;userid=AapfqIzytwl7ks8AA_qiU_BNUs8AAAFYqnZh4Q'
try:
req = urllib2.Request(uri, headers={ 'User-Agent': 'Mozilla/5.0' })
file = urllib2.urlopen(req)
except urllib2.HTTPError as err:
print('HTTP error!!!')
file = err
print(err.code)
except urllib2.URLError as err:
print('URL error!!!')
print(err.reason)
return
data = file.read(1024)
print(data)
脚本结束后,数据仍为空。为什么会发生?没有HTTPError,我可以在浏览器控制台中看到有一个有效的gif,并且HTTP responce的状态为200 OK。谢谢。
我能不能从这个网站得到有效的cookies?看来urllib2.urlopen(req)不能接收cookies? –
与'urllib.request',你将不得不使用'CookieJar'类,但它需要更多的工作 - 所以我总是使用'request'模块。 – furas