import urllib2
def download(url,user_agent = 'wswp',num_retries=2):
print 'downloading:',url
headers = {'User-Agent': 'Mozilla/5.0'}
request = urllib2.Request(url,headers=headers)
try:
html = urllib2.urlopen(request).read()
except urllib2.URLError as e:
print "download error:"
html = None
if num_retries>0:
if hasattr(e,'code') and 500<=e.code<600:
print "e.code = ",e.code
return download(url,num_retries-1)
return html
print download("http://www.huaru.cc/mobile/product/xsim.html")
结果工作\ Python27 \ python.exe E:/ py2_7 /未命名/ secondClass_Agent下载: http://www.huaru.cc/mobile/product/xsim.html蟒的urllib2无法在某些现场
过程,退出代码完成0
在我的机器上工作。我修好了缩进之后。 –
也适用于我的机器。检查你的缩进。 – sangheestyle
嗨,你的意思是你可以下载本网站的所有代码?你能粘贴你的结果吗?谢谢。 –