getFreeProxy.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. """
  4. -------------------------------------------------
  5. File Name: GetFreeProxy.py
  6. Description : 抓取免费代理
  7. Author : JHao
  8. date: 2016/11/25
  9. -------------------------------------------------
  10. Change Activity:
  11. 2016/11/25:
  12. -------------------------------------------------
  13. """
  14. import re
  15. import requests
  16. try:
  17. from importlib import reload # py3 实际不会实用,只是为了不显示语法错误
  18. except:
  19. import sys # py2
  20. reload(sys)
  21. sys.setdefaultencoding('utf-8')
  22. from Util.utilFunction import robustCrawl, getHtmlTree
  23. from Util.WebRequest import WebRequest
  24. # for debug to disable insecureWarning
  25. requests.packages.urllib3.disable_warnings()
  26. class GetFreeProxy(object):
  27. """
  28. proxy getter
  29. """
  30. def __init__(self):
  31. pass
  32. @staticmethod
  33. @robustCrawl # decoration print error if exception happen
  34. def freeProxyFirst(page=10):
  35. """
  36. 抓取无忧代理 http://www.data5u.com/
  37. :param page: 页数
  38. :return:
  39. """
  40. url_list = ['http://www.data5u.com/',
  41. 'http://www.data5u.com/free/',
  42. 'http://www.data5u.com/free/gngn/index.shtml',
  43. 'http://www.data5u.com/free/gnpt/index.shtml']
  44. for url in url_list:
  45. html_tree = getHtmlTree(url)
  46. ul_list = html_tree.xpath('//ul[@class="l2"]')
  47. for ul in ul_list:
  48. yield ':'.join(ul.xpath('.//li/text()')[0:2])
  49. @staticmethod
  50. @robustCrawl
  51. def freeProxySecond(proxy_number=100):
  52. """
  53. 抓取代理66 http://www.66ip.cn/
  54. :param proxy_number: 代理数量
  55. :return:
  56. """
  57. url = "http://www.66ip.cn/mo.php?sxb=&tqsl={}&port=&export=&ktip=&sxa=&submit=%CC%E1++%C8%A1&textarea=".format(
  58. proxy_number)
  59. request = WebRequest()
  60. # html = request.get(url).content
  61. # content为未解码,text为解码后的字符串
  62. html = request.get(url).text
  63. for proxy in re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}', html):
  64. yield proxy
  65. @staticmethod
  66. @robustCrawl
  67. def freeProxyThird(days=1):
  68. """
  69. 抓取ip181 http://www.ip181.com/
  70. :param days:
  71. :return:
  72. """
  73. url = 'http://www.ip181.com/'
  74. html_tree = getHtmlTree(url)
  75. tr_list = html_tree.xpath('//tr')[1:]
  76. for tr in tr_list:
  77. yield ':'.join(tr.xpath('./td/text()')[0:2])
  78. @staticmethod
  79. @robustCrawl
  80. def freeProxyFourth():
  81. """
  82. 抓取西刺代理 http://api.xicidaili.com/free2016.txt
  83. :return:
  84. """
  85. url_list = ['http://www.xicidaili.com/nn', # 高匿
  86. 'http://www.xicidaili.com/nt', # 透明
  87. ]
  88. for each_url in url_list:
  89. tree = getHtmlTree(each_url)
  90. proxy_list = tree.xpath('.//table[@id="ip_list"]//tr')
  91. for proxy in proxy_list:
  92. yield ':'.join(proxy.xpath('./td/text()')[0:2])
  93. @staticmethod
  94. @robustCrawl
  95. def freeProxyFifth():
  96. """
  97. 抓取guobanjia http://www.goubanjia.com/free/gngn/index.shtml
  98. :return:
  99. """
  100. url = "http://www.goubanjia.com/free/gngn/index{page}.shtml"
  101. for page in range(1, 10):
  102. page_url = url.format(page=page)
  103. tree = getHtmlTree(page_url)
  104. proxy_list = tree.xpath('//td[@class="ip"]')
  105. # 此网站有隐藏的数字干扰,或抓取到多余的数字或.符号
  106. # 需要过滤掉<p style="display:none;">的内容
  107. xpath_str = """.//*[not(contains(@style, 'display: none'))
  108. and not(contains(@style, 'display:none'))
  109. and not(contains(@class, 'port'))
  110. ]/text()
  111. """
  112. for each_proxy in proxy_list:
  113. # :符号裸放在td下,其他放在div span p中,先分割找出ip,再找port
  114. ip_addr = ''.join(each_proxy.xpath(xpath_str))
  115. port = each_proxy.xpath(".//span[contains(@class, 'port')]/text()")[0]
  116. yield '{}:{}'.format(ip_addr, port)
  117. if __name__ == '__main__':
  118. gg = GetFreeProxy()
  119. # for e in gg.freeProxyFirst():
  120. # print e
  121. # for e in gg.freeProxySecond():
  122. # print e
  123. # for e in gg.freeProxyThird():
  124. # print e
  125. # for e in gg.freeProxyFourth():
  126. # print(e)
  127. for e in gg.freeProxyFifth():
  128. print(e)