getFreeProxy.py 8.1 KB

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