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")
the result : C:\Python27\python.exe E:/py2_7/untitled1/secondClass_Agent downloading: http://www.huaru.cc/mobile/product/xsim.html
Process finished with exit code 0
在Python中,缩进是关键。你知道吗
它显示如下:
这是因为网页是返回404。你知道吗
它在python2.7.10和3.6上进行了测试
检查PEP8:https://www.python.org/dev/peps/pep-0008/#id17
相关问题 更多 >
编程相关推荐