utilFunction.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. """
  4. -------------------------------------------------
  5. File Name: utilFunction.py
  6. Description : tool function
  7. Author : JHao
  8. date: 2016/11/25
  9. -------------------------------------------------
  10. Change Activity:
  11. 2016/11/25: 添加robustCrawl、verifyProxy、getHtmlTree
  12. -------------------------------------------------
  13. """
  14. from lxml import etree
  15. from Util.WebRequest import WebRequest
  16. from .validators import validators
  17. from Config.ConfigGetter import config
  18. def robustCrawl(func):
  19. def decorate(*args, **kwargs):
  20. try:
  21. return func(*args, **kwargs)
  22. except Exception as e:
  23. pass
  24. # logger.info(u"sorry, 抓取出错。错误原因:")
  25. # logger.info(e)
  26. return decorate
  27. def verifyProxyFormat(proxy):
  28. """
  29. 检查代理格式
  30. :param proxy:
  31. :return:
  32. """
  33. import re
  34. verify_regex = r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}"
  35. _proxy = re.findall(verify_regex, proxy)
  36. return True if len(_proxy) == 1 and _proxy[0] == proxy else False
  37. def getHtmlTree(url, **kwargs):
  38. """
  39. 获取html树
  40. :param url:
  41. :param kwargs:
  42. :return:
  43. """
  44. header = {'Connection': 'keep-alive',
  45. 'Cache-Control': 'max-age=0',
  46. 'Upgrade-Insecure-Requests': '1',
  47. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko)',
  48. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  49. 'Accept-Encoding': 'gzip, deflate, sdch',
  50. 'Accept-Language': 'zh-CN,zh;q=0.8',
  51. }
  52. # TODO 取代理服务器用代理服务器访问
  53. wr = WebRequest()
  54. html = wr.get(url=url, header=header).content
  55. return etree.HTML(html)
  56. def tcpConnect(proxy):
  57. """
  58. TCP 三次握手
  59. :param proxy:
  60. :return:
  61. """
  62. from socket import socket, AF_INET, SOCK_STREAM
  63. s = socket(AF_INET, SOCK_STREAM)
  64. ip, port = proxy.split(':')
  65. result = s.connect_ex((ip, int(port)))
  66. return True if result == 0 else False
  67. def validUsefulProxy(proxy):
  68. """
  69. 检验代理是否可用
  70. :param proxy:
  71. :return:
  72. """
  73. if isinstance(proxy, bytes):
  74. proxy = proxy.decode("utf8")
  75. proxies = {"http": "http://{proxy}".format(proxy=proxy)}
  76. headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0',
  77. 'Accept': '*/*',
  78. 'Connection': 'keep-alive',
  79. 'Accept-Language': 'zh-CN,zh;q=0.8'}
  80. try:
  81. r = requests.head(config.verify_host, headers=headers, proxies=proxies, timeout=10, verify=False)
  82. if r.status_code == 200:
  83. return True
  84. except Exception as e:
  85. pass
  86. return False
  87. for v_func in validators:
  88. if not v_func(proxy):
  89. return False
  90. return True