普通做法:
# 第一种方式 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 模块
# 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 模块
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
)
|