getFreeProxy.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. def freeProxyFirst(page=10):
  34. """
  35. 抓取无忧代理 http://www.data5u.com/
  36. :param page: 页数
  37. :return:
  38. """
  39. url_list = ['http://www.data5u.com/',
  40. 'http://www.data5u.com/free/',
  41. 'http://www.data5u.com/free/gngn/index.shtml',
  42. 'http://www.data5u.com/free/gnpt/index.shtml']
  43. for url in url_list:
  44. html_tree = getHtmlTree(url)
  45. ul_list = html_tree.xpath('//ul[@class="l2"]')
  46. for ul in ul_list:
  47. try:
  48. yield ':'.join(ul.xpath('.//li/text()')[0:2])
  49. except Exception as e:
  50. pass
  51. @staticmethod
  52. def freeProxySecond(proxy_number=100):
  53. """
  54. 抓取代理66 http://www.66ip.cn/
  55. :param proxy_number: 代理数量
  56. :return:
  57. """
  58. url = "http://www.66ip.cn/mo.php?sxb=&tqsl={}&port=&export=&ktip=&sxa=&submit=%CC%E1++%C8%A1&textarea=".format(
  59. proxy_number)
  60. request = WebRequest()
  61. # html = request.get(url).content
  62. # content为未解码,text为解码后的字符串
  63. html = request.get(url).text
  64. for proxy in re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}', html):
  65. yield proxy
  66. @staticmethod
  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. try:
  76. tr_list = html_tree.xpath('//tr')[1:]
  77. for tr in tr_list:
  78. yield ':'.join(tr.xpath('./td/text()')[0:2])
  79. except Exception as e:
  80. pass
  81. @staticmethod
  82. def freeProxyFourth():
  83. """
  84. 抓取西刺代理 http://api.xicidaili.com/free2016.txt
  85. :return:
  86. """
  87. url_list = ['http://www.xicidaili.com/nn', # 高匿
  88. 'http://www.xicidaili.com/nt', # 透明
  89. ]
  90. for each_url in url_list:
  91. tree = getHtmlTree(each_url)
  92. proxy_list = tree.xpath('.//table[@id="ip_list"]//tr')
  93. for proxy in proxy_list:
  94. try:
  95. yield ':'.join(proxy.xpath('./td/text()')[0:2])
  96. except Exception as e:
  97. pass
  98. @staticmethod
  99. def freeProxyFifth():
  100. """
  101. 抓取guobanjia http://www.goubanjia.com/free/gngn/index.shtml
  102. :return:
  103. """
  104. url = "http://www.goubanjia.com/free/gngn/index{page}.shtml"
  105. for page in range(1, 10):
  106. page_url = url.format(page=page)
  107. tree = getHtmlTree(page_url)
  108. proxy_list = tree.xpath('//td[@class="ip"]')
  109. # 此网站有隐藏的数字干扰,或抓取到多余的数字或.符号
  110. # 需要过滤掉<p style="display:none;">的内容
  111. xpath_str = """.//*[not(contains(@style, 'display: none'))
  112. and not(contains(@style, 'display:none'))
  113. and not(contains(@class, 'port'))
  114. ]/text()
  115. """
  116. for each_proxy in proxy_list:
  117. try:
  118. # :符号裸放在td下,其他放在div span p中,先分割找出ip,再找port
  119. ip_addr = ''.join(each_proxy.xpath(xpath_str))
  120. port = each_proxy.xpath(".//span[contains(@class, 'port')]/text()")[0]
  121. yield '{}:{}'.format(ip_addr, port)
  122. except Exception as e:
  123. pass
  124. @staticmethod
  125. def freeProxySixth():
  126. """
  127. 抓取讯代理免费proxy http://www.xdaili.cn/ipagent/freeip/getFreeIps?page=1&rows=10
  128. :return:
  129. """
  130. url = 'http://www.xdaili.cn/ipagent/freeip/getFreeIps?page=1&rows=10'
  131. request = WebRequest()
  132. try:
  133. res = request.get(url).json()
  134. for row in res['RESULT']['rows']:
  135. yield '{}:{}'.format(row['ip'], row['port'])
  136. except Exception as e:
  137. pass
  138. @staticmethod
  139. def freeProxySeventh():
  140. """
  141. 快代理免费https://www.kuaidaili.com/free/inha/1/
  142. """
  143. url = 'https://www.kuaidaili.com/free/inha/{page}/'
  144. for page in range(1, 10):
  145. page_url = url.format(page=page)
  146. tree = getHtmlTree(page_url)
  147. proxy_list = tree.xpath('.//table//tr')
  148. for tr in proxy_list[1:]:
  149. yield ':'.join(tr.xpath('./td/text()')[0:2])
  150. if __name__ == '__main__':
  151. gg = GetFreeProxy()
  152. # for e in gg.freeProxyFirst():
  153. # print(e)
  154. #
  155. # for e in gg.freeProxySecond():
  156. # print(e)
  157. #
  158. # for e in gg.freeProxyThird():
  159. # print(e)
  160. # for e in gg.freeProxyFourth():
  161. # print(e)
  162. #for e in gg.freeProxyFifth():
  163. # print(e)
  164. # for e in gg.freeProxySixth():
  165. # print(e)
  166. for e in gg.freeProxySeventh():
  167. print(e)