-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathhelper.py
54 lines (48 loc) · 1.33 KB
/
helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 19-7-4 下午5:55
# @Author : Hubery
# @File : helper.py
# @Software: PyCharm
import time
import requests
from requests.exceptions import ConnectionError
from settings import BASE_HEADERS
def retry(max_tries=3, wait=5):
"""
获取失败,进行再次爬取
:param max_tries: 失败次数
:param wait: 每次失败时等待时间
:return:
"""
def deco(fun):
def wrapper(*args, **kwargs):
for i in range(max_tries):
result = fun(*args, **kwargs)
if result is None:
print('retry%s' %i)
time.sleep(wait)
continue
else:
return result
return wrapper
return deco
@retry()
def get_text(url, options={}):
"""
:param method: 请求方法
:param url: 请求的目标url
:param options:添加的请求头
:return:
"""
headers = dict(BASE_HEADERS, **options)
print('正在抓取', url)
try:
res = requests.get(url, headers=headers, timeout=5)
# print(res.status_code)
if res.status_code == 200:
print('抓取成功', url, res.status_code)
return res
except ConnectionError:
print('抓取失败', url)
return None