This commit is contained in:
caiwx86 2022-12-11 22:16:05 +08:00
parent 234afcff7a
commit 8276619657

View File

@ -25,23 +25,18 @@ class htmlUtils:
count = 1 count = 1
#数据为空则获取数据 #数据为空则获取数据
while url_text == None: while url_text == None:
if count <= 10: try:
try: print("请求地址:",curl)
print("请求地址:",curl) res = s.get(curl,stream=True, headers=cls.headers, timeout=50)
res = s.get(curl,stream=True, headers=cls.headers, timeout=50) if type == "bytes":
if type == "bytes": url_text = res
url_text = res else:
else: url_text = html.fromstring(res.text)
url_text = html.fromstring(res.text) except:
except: print(f'Retry! 第{count}')
print(f'Retry! 第{count}') time.sleep(3)
time.sleep(3) traceback.print_exc()
traceback.print_exc() continue
count += 1
continue
else:
print(f"fail 请求失败:{curl}")
break
if type == "None": if type == "None":
data = { curl : url_text} data = { curl : url_text}
cls.url_data.update(data) cls.url_data.update(data)
@ -53,13 +48,15 @@ class htmlUtils:
@classmethod @classmethod
def getJSON(cls,curl): def getJSON(cls,curl):
while True: count = 0
while count <= 5:
try: try:
res = requests.get(curl, headers=cls.headers,timeout=50) res = requests.get(curl, headers=cls.headers,timeout=50)
data_json = res.json() data_json = res.json()
return data_json return data_json
except: except:
print(f"请求失败,重试中...") print(f"请求失败,重试中...")
count += 1
time.sleep(1) time.sleep(1)
cls.getJSON(curl) cls.getJSON(curl)