Explorar o código

fixed:修复因代理请求超时,导致进程崩溃问题

dzr hai 9 meses
pai
achega
0e20f57abe
Modificáronse 2 ficheiros con 6 adicións e 6 borrados
  1. 3 3
      news_list.py
  2. 3 3
      tools.py

+ 3 - 3
news_list.py

@@ -69,12 +69,12 @@ def get_url(key, page_num, follow_redirects=False):
         headers=headers,
         params=params,
         timeout=10,
-        proxies=get_proxy(),
         follow_redirects=follow_redirects
     )
     try:
-        return httpx.get(url, **request_kwargs)
-    except httpx.HTTPError as e:
+        proxies = get_proxy()
+        return httpx.get(url, proxies=proxies, **request_kwargs)
+    except Exception as e:
         logger.exception(e)
 
 

+ 3 - 3
tools.py

@@ -37,12 +37,12 @@ def sha1(text: str):
 
 
 def get_proxy():
-    headers = {"Authorization": "Basic amlhbnl1MDAxOjEyM3F3ZSFB"}
+    headers = {"authorization": "Basic amlhbnl1MDAxOjEyM3F3ZSFB"}
     url = "http://cc.spdata.jianyu360.com/crawl/proxy/socks5/fetch"
-    proxy = requests.get(url, headers=headers, timeout=3).json()
+    proxy = requests.get(url, headers=headers, timeout=5).json()
     proxy = proxy.get("data")
     logging.info("切换代理:{}".format(proxy))
     if not proxy:
-        return
+        raise ValueError('未获取代理ip')
 
     return proxy.get("http")