getFreeProxy.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. sys.path.append('..')
  18. from Util.WebRequest import WebRequest
  19. from Util.utilFunction import getHtmlTree
  20. # for debug to disable insecureWarning
  21. requests.packages.urllib3.disable_warnings()
  22. class GetFreeProxy(object):
  23. """
  24. proxy getter
  25. """
  26. @staticmethod
  27. def freeProxy01():
  28. """
  29. 无忧代理 http://www.data5u.com/
  30. 几乎没有能用的
  31. :return:
  32. """
  33. url_list = [
  34. 'http://www.data5u.com/',
  35. ]
  36. for url in url_list:
  37. html_tree = getHtmlTree(url)
  38. ul_list = html_tree.xpath('//ul[@class="l2"]')
  39. for ul in ul_list:
  40. try:
  41. yield ':'.join(ul.xpath('.//li/text()')[0:2])
  42. except Exception as e:
  43. print(e)
  44. @staticmethod
  45. def freeProxySecond(count=20):
  46. """
  47. 代理66 http://www.66ip.cn/
  48. :param count: 提取数量
  49. :return:
  50. """
  51. urls = [
  52. "http://www.66ip.cn/mo.php?sxb=&tqsl={count}&port=&export=&ktip=&sxa=&submit=%CC%E1++%C8%A1&textarea=",
  53. "http://www.66ip.cn/nmtq.php?getnum={count}"
  54. "&isp=0&anonymoustype=0&start=&ports=&export=&ipaddress=&area=1&proxytype=2&api=66ip",
  55. ]
  56. request = WebRequest()
  57. for _ in urls:
  58. url = _.format(count=count)
  59. html = request.get(url).content
  60. ips = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}", html)
  61. for ip in ips:
  62. yield ip.strip()
  63. @staticmethod
  64. def freeProxyThird(days=1):
  65. """
  66. ip181 http://www.ip181.com/ 不能用了
  67. :param days:
  68. :return:
  69. """
  70. url = 'http://www.ip181.com/'
  71. html_tree = getHtmlTree(url)
  72. try:
  73. tr_list = html_tree.xpath('//tr')[1:]
  74. for tr in tr_list:
  75. yield ':'.join(tr.xpath('./td/text()')[0:2])
  76. except Exception as e:
  77. pass
  78. @staticmethod
  79. def freeProxyFourth(page_count=1):
  80. """
  81. 西刺代理 http://www.xicidaili.com
  82. :return:
  83. """
  84. url_list = [
  85. 'http://www.xicidaili.com/nn/', # 高匿
  86. 'http://www.xicidaili.com/nt/', # 透明
  87. ]
  88. for each_url in url_list:
  89. for i in range(1, page_count + 1):
  90. page_url = each_url + str(i)
  91. tree = getHtmlTree(page_url)
  92. proxy_list = tree.xpath('.//table[@id="ip_list"]//tr[position()>1]')
  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/
  102. :return:
  103. """
  104. url = "http://www.goubanjia.com/"
  105. tree = getHtmlTree(url)
  106. proxy_list = tree.xpath('//td[@class="ip"]')
  107. # 此网站有隐藏的数字干扰,或抓取到多余的数字或.符号
  108. # 需要过滤掉<p style="display:none;">的内容
  109. xpath_str = """.//*[not(contains(@style, 'display: none'))
  110. and not(contains(@style, 'display:none'))
  111. and not(contains(@class, 'port'))
  112. ]/text()
  113. """
  114. for each_proxy in proxy_list:
  115. try:
  116. # :符号裸放在td下,其他放在div span p中,先分割找出ip,再找port
  117. ip_addr = ''.join(each_proxy.xpath(xpath_str))
  118. # HTML中的port是随机数,真正的端口编码在class后面的字母中。
  119. # 比如这个:
  120. # <span class="port CFACE">9054</span>
  121. # CFACE解码后对应的是3128。
  122. port = 0
  123. for _ in each_proxy.xpath(".//span[contains(@class, 'port')]"
  124. "/attribute::class")[0]. \
  125. replace("port ", ""):
  126. port *= 10
  127. port += (ord(_) - ord('A'))
  128. port /= 8
  129. yield '{}:{}'.format(ip_addr, int(port))
  130. except Exception as e:
  131. pass
  132. @staticmethod
  133. def freeProxySixth():
  134. """
  135. 讯代理 http://www.xdaili.cn/ 已停用
  136. :return:
  137. """
  138. url = 'http://www.xdaili.cn/ipagent/freeip/getFreeIps?page=1&rows=10'
  139. request = WebRequest()
  140. try:
  141. res = request.get(url, timeout=10).json()
  142. for row in res['RESULT']['rows']:
  143. yield '{}:{}'.format(row['ip'], row['port'])
  144. except Exception as e:
  145. pass
  146. @staticmethod
  147. def freeProxySeventh():
  148. """
  149. 快代理 https://www.kuaidaili.com
  150. """
  151. url_list = [
  152. 'https://www.kuaidaili.com/free/inha/',
  153. 'https://www.kuaidaili.com/free/intr/'
  154. ]
  155. for url in url_list:
  156. tree = getHtmlTree(url)
  157. proxy_list = tree.xpath('.//table//tr')
  158. for tr in proxy_list[1:]:
  159. yield ':'.join(tr.xpath('./td/text()')[0:2])
  160. @staticmethod
  161. def freeProxyEight():
  162. """
  163. 秘密代理 http://www.mimiip.com 已停用
  164. """
  165. url_gngao = ['http://www.mimiip.com/gngao/%s' % n for n in range(1, 2)] # 国内高匿
  166. url_gnpu = ['http://www.mimiip.com/gnpu/%s' % n for n in range(1, 2)] # 国内普匿
  167. url_gntou = ['http://www.mimiip.com/gntou/%s' % n for n in range(1, 2)] # 国内透明
  168. url_list = url_gngao + url_gnpu + url_gntou
  169. request = WebRequest()
  170. for url in url_list:
  171. r = request.get(url, timeout=10)
  172. 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)
  173. for proxy in proxies:
  174. yield ':'.join(proxy)
  175. @staticmethod
  176. def freeProxyNinth():
  177. """
  178. 码农代理 https://proxy.coderbusy.com/ 已停用
  179. :return:
  180. """
  181. urls = ['https://proxy.coderbusy.com/classical/country/cn.aspx?page=1']
  182. request = WebRequest()
  183. for url in urls:
  184. r = request.get(url, timeout=10)
  185. proxies = re.findall('data-ip="(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})".+?>(\d+)</td>', r.text)
  186. for proxy in proxies:
  187. yield ':'.join(proxy)
  188. @staticmethod
  189. def freeProxyTen():
  190. """
  191. 云代理 http://www.ip3366.net/free/
  192. :return:
  193. """
  194. urls = ['http://www.ip3366.net/free/']
  195. request = WebRequest()
  196. for url in urls:
  197. r = request.get(url, timeout=10)
  198. proxies = re.findall(r'<td>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})</td>[\s\S]*?<td>(\d+)</td>', r.text)
  199. for proxy in proxies:
  200. yield ":".join(proxy)
  201. @staticmethod
  202. def freeProxyEleven():
  203. """
  204. IP海 http://www.iphai.com/free/ng
  205. :return:
  206. """
  207. urls = [
  208. 'http://www.iphai.com/free/ng',
  209. 'http://www.iphai.com/free/np',
  210. 'http://www.iphai.com/free/wg',
  211. 'http://www.iphai.com/free/wp'
  212. ]
  213. request = WebRequest()
  214. for url in urls:
  215. r = request.get(url, timeout=10)
  216. proxies = re.findall(r'<td>\s*?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s*?</td>[\s\S]*?<td>\s*?(\d+)\s*?</td>',
  217. r.text)
  218. for proxy in proxies:
  219. yield ":".join(proxy)
  220. @staticmethod
  221. def freeProxyTwelve(page_count=2):
  222. """
  223. http://ip.jiangxianli.com/?page=
  224. 免费代理库
  225. 超多量
  226. :return:
  227. """
  228. for i in range(1, page_count + 1):
  229. url = 'http://ip.jiangxianli.com/?page={}'.format(i)
  230. html_tree = getHtmlTree(url)
  231. tr_list = html_tree.xpath("/html/body/div[1]/div/div[1]/div[2]/table/tbody/tr")
  232. if len(tr_list) == 0:
  233. continue
  234. for tr in tr_list:
  235. yield tr.xpath("./td[2]/text()")[0] + ":" + tr.xpath("./td[3]/text()")[0]
  236. @staticmethod
  237. def freeProxyWallFirst():
  238. """
  239. 墙外网站 cn-proxy
  240. :return:
  241. """
  242. urls = ['http://cn-proxy.com/', 'http://cn-proxy.com/archives/218']
  243. request = WebRequest()
  244. for url in urls:
  245. r = request.get(url, timeout=10)
  246. 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)
  247. for proxy in proxies:
  248. yield ':'.join(proxy)
  249. @staticmethod
  250. def freeProxyWallSecond():
  251. """
  252. https://proxy-list.org/english/index.php
  253. :return:
  254. """
  255. urls = ['https://proxy-list.org/english/index.php?p=%s' % n for n in range(1, 10)]
  256. request = WebRequest()
  257. import base64
  258. for url in urls:
  259. r = request.get(url, timeout=10)
  260. proxies = re.findall(r"Proxy\('(.*?)'\)", r.text)
  261. for proxy in proxies:
  262. yield base64.b64decode(proxy).decode()
  263. @staticmethod
  264. def freeProxyWallThird():
  265. urls = ['https://list.proxylistplus.com/Fresh-HTTP-Proxy-List-1']
  266. request = WebRequest()
  267. for url in urls:
  268. r = request.get(url, timeout=10)
  269. proxies = re.findall(r'<td>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})</td>[\s\S]*?<td>(\d+)</td>', r.text)
  270. for proxy in proxies:
  271. yield ':'.join(proxy)
  272. if __name__ == '__main__':
  273. from CheckProxy import CheckProxy
  274. CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy01())
  275. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxySecond)
  276. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxyThird)
  277. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxyFourth)
  278. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxyFifth)
  279. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxySixth)
  280. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxySeventh)
  281. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxyEight)
  282. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxyNinth)
  283. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxyTen)
  284. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxyEleven)
  285. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxyTwelve)
  286. # CheckProxy.checkAllGetProxyFunc()