getFreeProxy.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 sys
  16. import requests
  17. try:
  18. from importlib import reload # py3 实际不会实用,只是为了不显示语法错误
  19. except:
  20. reload(sys)
  21. sys.setdefaultencoding('utf-8')
  22. sys.path.append('../')
  23. from Util.utilFunction import robustCrawl, getHtmlTree
  24. from Util.WebRequest import WebRequest
  25. # for debug to disable insecureWarning
  26. requests.packages.urllib3.disable_warnings()
  27. """
  28. 66ip.cn
  29. data5u.com
  30. ip181.com
  31. xicidaili.com
  32. goubanjia.com
  33. xdaili.cn
  34. kuaidaili.com
  35. cn-proxy.com
  36. proxy-list.org
  37. www.mimiip.com
  38. """
  39. class GetFreeProxy(object):
  40. """
  41. proxy getter
  42. """
  43. def __init__(self):
  44. pass
  45. @staticmethod
  46. def freeProxyFirst(page=10):
  47. """
  48. 抓取无忧代理 http://www.data5u.com/
  49. :param page: 页数
  50. :return:
  51. """
  52. url_list = ['http://www.data5u.com/',
  53. 'http://www.data5u.com/free/',
  54. 'http://www.data5u.com/free/gngn/index.shtml',
  55. 'http://www.data5u.com/free/gnpt/index.shtml']
  56. for url in url_list:
  57. html_tree = getHtmlTree(url)
  58. ul_list = html_tree.xpath('//ul[@class="l2"]')
  59. for ul in ul_list:
  60. try:
  61. yield ':'.join(ul.xpath('.//li/text()')[0:2])
  62. except Exception as e:
  63. pass
  64. @staticmethod
  65. def freeProxySecond(proxy_number=100):
  66. """
  67. 抓取代理66 http://www.66ip.cn/
  68. :param proxy_number: 代理数量
  69. :return:
  70. """
  71. url = "http://www.66ip.cn/mo.php?sxb=&tqsl={}&port=&export=&ktip=&sxa=&submit=%CC%E1++%C8%A1&textarea=".format(
  72. proxy_number)
  73. request = WebRequest()
  74. html = request.get(url).text
  75. for proxy in re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}', html):
  76. yield proxy
  77. @staticmethod
  78. def freeProxyThird(days=1):
  79. """
  80. 抓取ip181 http://www.ip181.com/
  81. :param days:
  82. :return:
  83. """
  84. url = 'http://www.ip181.com/'
  85. html_tree = getHtmlTree(url)
  86. try:
  87. tr_list = html_tree.xpath('//tr')[1:]
  88. for tr in tr_list:
  89. yield ':'.join(tr.xpath('./td/text()')[0:2])
  90. except Exception as e:
  91. pass
  92. @staticmethod
  93. def freeProxyFourth():
  94. """
  95. 抓取西刺代理 http://api.xicidaili.com/free2016.txt
  96. :return:
  97. """
  98. url_list = ['http://www.xicidaili.com/nn', # 高匿
  99. 'http://www.xicidaili.com/nt', # 透明
  100. ]
  101. for each_url in url_list:
  102. tree = getHtmlTree(each_url)
  103. proxy_list = tree.xpath('.//table[@id="ip_list"]//tr')
  104. for proxy in proxy_list:
  105. try:
  106. yield ':'.join(proxy.xpath('./td/text()')[0:2])
  107. except Exception as e:
  108. pass
  109. @staticmethod
  110. def freeProxyFifth():
  111. """
  112. 抓取guobanjia http://www.goubanjia.com/
  113. :return:
  114. """
  115. url = "http://www.goubanjia.com/"
  116. tree = getHtmlTree(url)
  117. proxy_list = tree.xpath('//td[@class="ip"]')
  118. # 此网站有隐藏的数字干扰,或抓取到多余的数字或.符号
  119. # 需要过滤掉<p style="display:none;">的内容
  120. xpath_str = """.//*[not(contains(@style, 'display: none'))
  121. and not(contains(@style, 'display:none'))
  122. and not(contains(@class, 'port'))
  123. ]/text()
  124. """
  125. for each_proxy in proxy_list:
  126. try:
  127. # :符号裸放在td下,其他放在div span p中,先分割找出ip,再找port
  128. ip_addr = ''.join(each_proxy.xpath(xpath_str))
  129. port = each_proxy.xpath(".//span[contains(@class, 'port')]/text()")[0]
  130. yield '{}:{}'.format(ip_addr, port)
  131. except Exception as e:
  132. pass
  133. @staticmethod
  134. def freeProxySixth():
  135. """
  136. 抓取讯代理免费proxy http://www.xdaili.cn/ipagent/freeip/getFreeIps?page=1&rows=10
  137. :return:
  138. """
  139. url = 'http://www.xdaili.cn/ipagent/freeip/getFreeIps?page=1&rows=10'
  140. request = WebRequest()
  141. try:
  142. res = request.get(url).json()
  143. for row in res['RESULT']['rows']:
  144. yield '{}:{}'.format(row['ip'], row['port'])
  145. except Exception as e:
  146. pass
  147. @staticmethod
  148. def freeProxySeventh():
  149. """
  150. 快代理免费https://www.kuaidaili.com/free/inha/1/
  151. """
  152. url = 'https://www.kuaidaili.com/free/inha/{page}/'
  153. for page in range(1, 10):
  154. page_url = url.format(page=page)
  155. tree = getHtmlTree(page_url)
  156. proxy_list = tree.xpath('.//table//tr')
  157. for tr in proxy_list[1:]:
  158. yield ':'.join(tr.xpath('./td/text()')[0:2])
  159. @staticmethod
  160. def freeProxyEight():
  161. """
  162. 秘密代理IP网站http://www.mimiip.com
  163. """
  164. url_gngao = ['http://www.mimiip.com/gngao/%s' % n for n in range(1, 10)] # 国内高匿
  165. url_gnpu = ['http://www.mimiip.com/gnpu/%s' % n for n in range(1, 10)] # 国内普匿
  166. url_gntou = ['http://www.mimiip.com/gntou/%s' % n for n in range(1, 10)] # 国内透明
  167. url_list = url_gngao + url_gnpu + url_gntou
  168. request = WebRequest()
  169. for url in url_list:
  170. r = request.get(url)
  171. proxies = re.findall(r'<td>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})</td>[\w\W].*<td>(\d+)</td>', r.text)
  172. for proxy in proxies:
  173. yield ':'.join(proxy)
  174. @staticmethod
  175. def freeProxyWallFirst():
  176. """
  177. 墙外网站 cn-proxy
  178. :return:
  179. """
  180. urls = ['http://cn-proxy.com/', 'http://cn-proxy.com/archives/218']
  181. request = WebRequest()
  182. for url in urls:
  183. r = request.get(url)
  184. proxies = re.findall(r'<td>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})</td>[\w\W]<td>(\d+)</td>', r.text)
  185. for proxy in proxies:
  186. yield ':'.join(proxy)
  187. @staticmethod
  188. def freeProxyWallSecond():
  189. urls = ['https://proxy-list.org/english/index.php?p=%s' % n for n in range(1, 10)]
  190. request = WebRequest()
  191. import base64
  192. for url in urls:
  193. r = request.get(url)
  194. proxies = re.findall(r"Proxy\('(.*?)'\)", r.text)
  195. for proxy in proxies:
  196. yield base64.b64decode(proxy).decode()
  197. if __name__ == '__main__':
  198. gg = GetFreeProxy()
  199. # for e in gg.freeProxyFirst():
  200. # print(e)
  201. #
  202. # for e in gg.freeProxySecond():
  203. # print(e)
  204. #
  205. # for e in gg.freeProxyThird():
  206. # print(e)
  207. #
  208. # for e in gg.freeProxyFourth():
  209. # print(e)
  210. #
  211. # for e in gg.freeProxyFifth():
  212. # print(e)
  213. #
  214. # for e in gg.freeProxySixth():
  215. # print(e)
  216. #
  217. # for e in gg.freeProxySeventh():
  218. # print(e)
  219. #
  220. # for e in gg.freeProxyEight():
  221. # print(e)
  222. #
  223. # for e in gg.freeProxyWallFirst():
  224. # print(e)
  225. #
  226. # for e in gg.freeProxyWallSecond():
  227. # print(e)