【发布时间】:2017-03-02 15:39:28
【问题描述】:
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")
结果:C:\Python27\python.exe E:/py2_7/untitled1/secondClass_Agent 下载: http://www.huaru.cc/mobile/product/xsim.html
进程以退出代码 0 结束
【问题讨论】:
-
在我的机器上工作。在我修复缩进之后。
-
也可以在我的机器上使用。检查你的缩进。
-
你好,你的意思是你可以下载这个网站的所有代码?你能粘贴你的结果吗?谢谢。
标签: python web-crawler urllib2