Explorar el Código

[update] 代理验证函数

jhao104 hace 9 años
padre
commit
f382272ecf
Se han modificado 1 ficheros con 18 adiciones y 0 borrados
  1. 18 0
      Util/utilFunction.py

+ 18 - 0
Util/utilFunction.py

@@ -11,6 +11,7 @@
                    2016/11/25: 添加robustCrawl、verifyProxy、getHtmlTree
 -------------------------------------------------
 """
+import requests
 
 
 # noinspection PyPep8Naming
@@ -55,3 +56,20 @@ def getHtmlTree(url, **kwargs):
               }
     html = requests.get(url=url, headers=header, timeout=30).content
     return etree.HTML(html)
+
+
+def validUsefulProxy(proxy):
+    """
+    检验代理可以性
+    :param proxy:
+    :return:
+    """
+    proxies = {"http": "http://{proxy}".format(proxy=proxy),
+               "https": "https://{proxy}".format(proxy=proxy)}
+    try:
+        # 超过30秒的代理就不要了
+        r = requests.get('https://www.baidu.com/', proxies=proxies, timeout=30, verify=False)
+        if r.status_code == 200:
+            return True
+    except Exception, e:
+        return False