在写爬虫的时候,难免会遇到报错,比如 4XX ,5XX,有些可能是网络的原因,或者一些其他的原因,这个时候我们希望程序去做第二次下载,

有一种很low的解决方案,比如是用  try  except

  

try:
-------
except:
try:
--------
except:
try:
------
except:
try:
------
except:
try:
------
except:
try:
------
except:
------

有没有看起来更舒服的写法呢?

我们可以用递归实现这个过程

代码如下

request_urls = [
"https://www.baidu.com/",
"https://www.baidu.com/",
"https://www.baidu.com/",
"https://www.ba111111idu.com/",
"https://www.baidu.com/",
"https://www.baidu.com/",
] def down_load(url,request_max=3):
print "正在请求的URL是:",url
result_html = ""
result_status_code = ""
try:
result = session.get(url=url)
result_html = result.content
result_status_code = result.status_code
print result_status_code
except Exception as e:
print e
if request_max >0:
if result_status_code != 200:
return down_load(url,request_max-1)
return result_html for url in request_urls:
down_load(url=url,request_max=13)

输出结果:

C:\Python27\python.exe C:/Users/xuchunlin/PycharmProjects/A9_25/auction/test.py
正在请求的URL是: https://www.baidu.com/
200
正在请求的URL是: https://www.baidu.com/
200
正在请求的URL是: https://www.baidu.com/
200
正在请求的URL是: https://www.ba111111idu.com/
HTTPSConnectionPool(host='www.ba111111idu.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x0000000003AA6208>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed',))
正在请求的URL是: https://www.ba111111idu.com/
HTTPSConnectionPool(host='www.ba111111idu.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x0000000003AA6438>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed',))
正在请求的URL是: https://www.ba111111idu.com/
HTTPSConnectionPool(host='www.ba111111idu.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x0000000003AA65F8>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed',))
正在请求的URL是: https://www.ba111111idu.com/
HTTPSConnectionPool(host='www.ba111111idu.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x0000000003AA6828>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed',))
正在请求的URL是: https://www.ba111111idu.com/
HTTPSConnectionPool(host='www.ba111111idu.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x0000000003AA6A90>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed',))
正在请求的URL是: https://www.ba111111idu.com/
HTTPSConnectionPool(host='www.ba111111idu.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x0000000003AA62E8>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed',))
正在请求的URL是: https://www.ba111111idu.com/
HTTPSConnectionPool(host='www.ba111111idu.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x0000000003AA6D30>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed',))
正在请求的URL是: https://www.ba111111idu.com/
HTTPSConnectionPool(host='www.ba111111idu.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x0000000003AA6DD8>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed',))
正在请求的URL是: https://www.ba111111idu.com/
HTTPSConnectionPool(host='www.ba111111idu.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x0000000003B682B0>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed',))
正在请求的URL是: https://www.ba111111idu.com/
HTTPSConnectionPool(host='www.ba111111idu.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x0000000003B68080>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed',))
正在请求的URL是: https://www.ba111111idu.com/
HTTPSConnectionPool(host='www.ba111111idu.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x0000000003B685C0>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed',))
正在请求的URL是: https://www.ba111111idu.com/
HTTPSConnectionPool(host='www.ba111111idu.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x0000000003B687F0>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed',))
正在请求的URL是: https://www.ba111111idu.com/
HTTPSConnectionPool(host='www.ba111111idu.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x0000000003B68A20>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed',))
正在请求的URL是: https://www.ba111111idu.com/
HTTPSConnectionPool(host='www.ba111111idu.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x0000000003B68C50>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed',))
正在请求的URL是: https://www.baidu.com/
200
正在请求的URL是: https://www.baidu.com/
200 Process finished with exit code 0

python 爬虫 重复下载 二次请求的更多相关文章

  1. Python爬虫入门(二)之Requests库

    Python爬虫入门(二)之Requests库 我是照着小白教程做的,所以该篇是更小白教程hhhhhhhh 一.Requests库的简介 Requests 唯一的一个非转基因的 Python HTTP ...

  2. Python爬虫初学(二)—— 爬百度贴吧

    Python爬虫初学(二)-- 爬百度贴吧 昨天初步接触了爬虫,实现了爬取网络段子并逐条阅读等功能,详见Python爬虫初学(一). 今天准备对百度贴吧下手了,嘿嘿.依然是跟着这个博客学习的,这次仿照 ...

  3. python爬虫脚本下载YouTube视频

    python爬虫脚本下载YouTube视频 爬虫 python YouTube视频 工作环境: python 2.7.13 pip lxml, 安装 pip install lxml,主要用xpath ...

  4. Python爬虫学习:二、爬虫的初步尝试

    我使用的编辑器是IDLE,版本为Python2.7.11,Windows平台. 本文是博主原创随笔,转载时请注明出处Maple2cat|Python爬虫学习:二.爬虫的初步尝试 1.尝试抓取指定网页 ...

  5. python爬虫之下载文件的方式总结以及程序实例

    python爬虫之下载文件的方式以及下载实例 目录 第一种方法:urlretrieve方法下载 第二种方法:request download 第三种方法:视频文件.大型文件下载 实战演示 第一种方法: ...

  6. Python爬虫小白---(二)爬虫基础--Selenium PhantomJS

    一.前言   前段时间尝试爬取了网易云音乐的歌曲,这次打算爬取QQ音乐的歌曲信息.网易云音乐歌曲列表是通过iframe展示的,可以借助Selenium获取到iframe的页面元素, 而QQ音乐采用的是 ...

  7. [记录][python]python爬虫,下载某图片网站的所有图集

    随笔仅用于学习交流,转载时请注明出处,http://www.cnblogs.com/CaDevil/p/5958770.html 该随笔是记录我的第一个python程序,一个爬去指定图片站点的所有图集 ...

  8. Python爬虫《爬取get请求的页面数据》

    一.urllib库 urllib是Python自带的一个用于爬虫的库,其主要作用就是可以通过代码模拟浏览器发送请求.其常被用到的子模块在Python3中的为urllib.request和urllib. ...

  9. Python 爬虫实战(二):使用 requests-html

    Python 爬虫实战(一):使用 requests 和 BeautifulSoup,我们使用了 requests 做网络请求,拿到网页数据再用 BeautifulSoup 解析,就在前不久,requ ...

随机推荐

  1. [译]AngularJS Services 获取后端数据

    原文:ANGULARJS SERVICES – FETCHING SERVER DATA $http是AngularJS内置的服务,能帮助我们完成从服务端获数据.简单的用法就是在你需要数据的时候,发起 ...

  2. hadoop 文件合并

    来自:http://blog.csdn.net/dandingyy/article/details/7490046 众所周知,Hadoop对处理单个大文件比处理多个小文件更有效率,另外单个文件也非常占 ...

  3. C# ListView用法

    ListView是个较为复杂的控件       1.定义   把它拽进来,系统会自动在Designer.cs里添加一个  this.listView1 = new System.Windows.For ...

  4. github使用入门

    连接地址 github使用入门 连接地址: https://zhuanlan.zhihu.com/p/21193604?refer=passer

  5. Spring Boot 之 RESTfull API简单项目的快速搭建(二)

    1.打包 -- Maven build 2.问题 [WARNING] The requested profile "pom.xml" could not be activated ...

  6. 排序基础之插入排序、冒泡排序、选择排序详解与Java代码实现

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6594533.html  一:插入排序==逐个往前相邻数比较交换,小的在前 第一轮:A[1]与A[0]比较,小的 ...

  7. java 判断String字符串是不是json数据

      java 判断String字符串是不是json数据 CreationTime--2018年8月24日18点23分 Author:Marydon JSONObject jo = null; try ...

  8. JUC-线程池

    一,问题 在没有使用线程池的时候,每次需要一个线程都得手动new Thread()方式创建线程,用完了再销毁. 我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发 ...

  9. mysql5.6特殊字符问题

    问题描述: 在搭建redis监控cache-cloud软件,发现对建立cache-cloud的库,无法删除 drop database cache-cloud; 很奇怪..... 问题解决: 百思不得 ...

  10. Eclipse 语言包下载

    1.登陆http://www.eclipse.org/babel/downloads.php 选择你的eclipse版本   2.找到IDE中文补丁包 INDIGO的地址如下:http://downl ...