From 827661965701a9d7cc9b90bafbf0488a5808cdea Mon Sep 17 00:00:00 2001 From: caiwx86 Date: Sun, 11 Dec 2022 22:16:05 +0800 Subject: [PATCH] fix --- utils/HtmlUtils.py | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/utils/HtmlUtils.py b/utils/HtmlUtils.py index fd19306..0578b4e 100644 --- a/utils/HtmlUtils.py +++ b/utils/HtmlUtils.py @@ -25,23 +25,18 @@ class htmlUtils: count = 1 #数据为空则获取数据 while url_text == None: - if count <= 10: - try: - print("请求地址:",curl) - res = s.get(curl,stream=True, headers=cls.headers, timeout=50) - if type == "bytes": - url_text = res - else: - url_text = html.fromstring(res.text) - except: - print(f'Retry! 第{count}次') - time.sleep(3) - traceback.print_exc() - count += 1 - continue - else: - print(f"fail 请求失败:{curl}") - break + try: + print("请求地址:",curl) + res = s.get(curl,stream=True, headers=cls.headers, timeout=50) + if type == "bytes": + url_text = res + else: + url_text = html.fromstring(res.text) + except: + print(f'Retry! 第{count}次') + time.sleep(3) + traceback.print_exc() + continue if type == "None": data = { curl : url_text} cls.url_data.update(data) @@ -53,13 +48,15 @@ class htmlUtils: @classmethod def getJSON(cls,curl): - while True: + count = 0 + while count <= 5: try: res = requests.get(curl, headers=cls.headers,timeout=50) data_json = res.json() return data_json except: print(f"请求失败,重试中...") + count += 1 time.sleep(1) cls.getJSON(curl)