utilFunction.py 2.9 KB

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