首页 > 编程语言 > 详细

python网络爬虫 - 设定重试次数内反复抓取

时间:2016-05-24 22:34:40      阅读:252      评论:0      收藏:0      [点我收藏+]
import urllib.request

def download(url, num_retries=2):
    print(‘Downloading:‘, url)
    try:
        html = urllib.request.urlopen(url).read()
    except urllib.URLError as e:
        print(‘Download error:‘ % e.reason)
        html = None
        if num_retries > 0:
            if hasattr(e, ‘code‘) and 500 <= e.code < 600:
                # recursively retry 5xx HTTP errors
                return download(url, num_retries-1)
    return html
    
url = ‘http://www.google.com‘
print(download(url, 3))

 

python网络爬虫 - 设定重试次数内反复抓取

原文:http://www.cnblogs.com/davidgu/p/5524799.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!