python 爬虫中常用的retry模块 retrying模块

普通做法:

Python
# 第一种方式 def do_some(url, n=1): print(n, url) if n > 2: print('<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/retry" title="View all posts in retry" target="_blank">retry</a></span> many times') return None try: r = requests.get(url, timeout=2) # if r.status_code !=200: return r.text except Exception as e: print(e.args) n += 1 return do_some(url, n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 第一种方式
 
def do_some ( url , n = 1 ) :
     print ( n , url )
     if n > 2 :
         print ( 'retry many times' )
         return None
     try :
         r = requests . get ( url , timeout = 2 )
         # if r.status_code !=200:
         return r . text
     except Exception as e :
         print ( e . args )
         n += 1
         return do_some ( url , n )
 
 

retry 模块

Python
# coding:utf-8 from requests.exceptions import ConnectTimeout import requests from <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/retry" title="View all posts in retry" target="_blank">retry</a></span> import __author__ = 'songhao' # 第二中方法 @retry(exceptions=ConnectTimeout, tries=3) def do_other(ourl): print(ourl, 'ddddddddddd') r = requests.get(ourl, timeout=1) return r.text if __name__ == '__main__': ourl = "https://www.google.com" do_some(ourl) try: do_other(ourl) except Exception as e: print("dssssssssss") pass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# coding:utf-8
from requests . exceptions import ConnectTimeout
import requests
from retry import
__author__ = 'songhao'
 
# 第二中方法
@ retry ( exceptions = ConnectTimeout , tries = 3 )
def do_other ( ourl ) :
     print ( ourl , 'ddddddddddd' )
     r = requests . get ( ourl , timeout = 1 )
     return r . text
 
 
if __name__ == '__main__' :
     ourl = "https://www.google.com"
     do_some ( ourl )
     try :
         do_other ( ourl )
     except Exception as e :
         print ( "dssssssssss" )
         pass
 

retrying 模块

Python
import requests from retrying import retry @retry(stop_max_attempt_number=2) def get_html(url): print(url) r = requests.get(url,timeout=2) return r.status_code if __name__ == "__main__": try: a = get_html("https://www.baidu.com") except Exception as e: print(e,'------------') if 'a' in locals().keys(): print(a) else: a = 0 print(a)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import requests
from retrying import retry
 
 
@ retry ( stop_max_attempt_number = 2 )
def get_html ( url ) :
     print ( url )
     r = requests . get ( url , timeout = 2 )
 
     return r . status_code
 
if __name__ == "__main__" :
     try :
         a = get_html ( "https://www.baidu.com" )
     except Exception as e :
         print ( e , '------------' )
     if 'a' in locals ( ) . keys ( ) :
         print ( a )
     else :
         a = 0
         print ( a )
 



  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值