This commit is contained in:
cwx committed 2023-01-09 20:24:08 +08:00
1 parent 8e6efe7c10
commit 368538e980
1 file changed
+7 -5
+7 -5
View File
@@ -68,6 +68,9 @@ class htmlUtils:
try: try:
print(f"请求地址:{curl}") print(f"请求地址:{curl}")
res = s.get(curl,stream=True, headers=cls.headers, timeout=180,allow_redirects=True) res = s.get(curl,stream=True, headers=cls.headers, timeout=180,allow_redirects=True)
if res.status_code != 200:
cls.remove_HtmlCache(curl)
return None
if type == "bytes": if type == "bytes":
url_text = res url_text = res
if type == "json": if type == "json":
@@ -77,8 +80,6 @@ class htmlUtils:
if type == None: if type == None:
url_text = html.fromstring(res.text) url_text = html.fromstring(res.text)
cls.saveHtml(curl,res.text) cls.saveHtml(curl,res.text)
if res.status_code != 200:
cls.remove_HtmlCache(curl)
except: except:
print(f'Retry! 第{count}次') print(f'Retry! 第{count}次')
time.sleep(1) time.sleep(1)
@@ -97,12 +98,13 @@ class htmlUtils:
@classmethod @classmethod
def getJSON(cls,url): def getJSON(cls,url):
url_text = requests.get(url,headers=cls.headers, timeout=180).text res = requests.get(url,headers=cls.headers, timeout=180)
if res.status_code != 200:
return None
try: try:
return json.loads(url_text) return json.loads(res.text)
except: except:
return None return None
#return cls.getHTML(url,type="json")
@classmethod @classmethod
def xpathData(cls,c_xpath,url=None,num=None,not_eq=None,update=False): def xpathData(cls,c_xpath,url=None,num=None,not_eq=None,update=False):