getFreeProxy.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 freeProxy02(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={}&port=&export=&ktip=&sxa=&submit=%CC%E1++%C8%A1&textarea=",
  53. "http://www.66ip.cn/nmtq.php?getnum={}&isp=0&anonymoustype=0&s"
  54. "tart=&ports=&export=&ipaddress=&area=0&proxytype=2&api=66ip"
  55. ]
  56. try:
  57. import execjs
  58. import requests
  59. headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0',
  60. 'Accept': '*/*',
  61. 'Connection': 'keep-alive',
  62. 'Accept-Language': 'zh-CN,zh;q=0.8'}
  63. session = requests.session()
  64. src = session.get("http://www.66ip.cn/", headers=headers).text
  65. src = src.split("</script>")[0] + '}'
  66. src = src.replace("<script>", "function test() {")
  67. src = src.replace("while(z++)try{eval(", ';var num=10;while(z++)try{var tmp=')
  68. src = src.replace(");break}", ";num--;if(tmp.search('cookie') != -1 | num<0){return tmp}}")
  69. ctx = execjs.compile(src)
  70. src = ctx.call("test")
  71. src = src[src.find("document.cookie="): src.find("};if((")]
  72. src = src.replace("document.cookie=", "")
  73. src = "function test() {var window={}; return %s }" % src
  74. cookie = execjs.compile(src).call('test')
  75. js_cookie = cookie.split(";")[0].split("=")[-1]
  76. except Exception as e:
  77. print(e)
  78. return
  79. for url in urls:
  80. try:
  81. html = session.get(url.format(count), cookies={"__jsl_clearance": js_cookie}, headers=headers).text
  82. ips = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}", html)
  83. for ip in ips:
  84. yield ip.strip()
  85. except Exception as e:
  86. print(e)
  87. pass
  88. @staticmethod
  89. def freeProxy03(page_count=1):
  90. """
  91. 西刺代理 http://www.xicidaili.com
  92. :return:
  93. """
  94. url_list = [
  95. 'http://www.xicidaili.com/nn/', # 高匿
  96. 'http://www.xicidaili.com/nt/', # 透明
  97. ]
  98. for each_url in url_list:
  99. for i in range(1, page_count + 1):
  100. page_url = each_url + str(i)
  101. tree = getHtmlTree(page_url)
  102. proxy_list = tree.xpath('.//table[@id="ip_list"]//tr[position()>1]')
  103. for proxy in proxy_list:
  104. try:
  105. yield ':'.join(proxy.xpath('./td/text()')[0:2])
  106. except Exception as e:
  107. pass
  108. @staticmethod
  109. def freeProxy04():
  110. """
  111. guobanjia http://www.goubanjia.com/
  112. :return:
  113. """
  114. url = "http://www.goubanjia.com/"
  115. tree = getHtmlTree(url)
  116. proxy_list = tree.xpath('//td[@class="ip"]')
  117. # 此网站有隐藏的数字干扰,或抓取到多余的数字或.符号
  118. # 需要过滤掉<p style="display:none;">的内容
  119. xpath_str = """.//*[not(contains(@style, 'display: none'))
  120. and not(contains(@style, 'display:none'))
  121. and not(contains(@class, 'port'))
  122. ]/text()
  123. """
  124. for each_proxy in proxy_list:
  125. try:
  126. # :符号裸放在td下,其他放在div span p中,先分割找出ip,再找port
  127. ip_addr = ''.join(each_proxy.xpath(xpath_str))
  128. # HTML中的port是随机数,真正的端口编码在class后面的字母中。
  129. # 比如这个:
  130. # <span class="port CFACE">9054</span>
  131. # CFACE解码后对应的是3128。
  132. port = 0
  133. for _ in each_proxy.xpath(".//span[contains(@class, 'port')]"
  134. "/attribute::class")[0]. \
  135. replace("port ", ""):
  136. port *= 10
  137. port += (ord(_) - ord('A'))
  138. port /= 8
  139. yield '{}:{}'.format(ip_addr, int(port))
  140. except Exception as e:
  141. pass
  142. @staticmethod
  143. def freeProxySixth():
  144. """
  145. 讯代理 http://www.xdaili.cn/ 已停用
  146. :return:
  147. """
  148. url = 'http://www.xdaili.cn/ipagent/freeip/getFreeIps?page=1&rows=10'
  149. request = WebRequest()
  150. try:
  151. res = request.get(url, timeout=10).json()
  152. for row in res['RESULT']['rows']:
  153. yield '{}:{}'.format(row['ip'], row['port'])
  154. except Exception as e:
  155. pass
  156. @staticmethod
  157. def freeProxySeventh():
  158. """
  159. 快代理 https://www.kuaidaili.com
  160. """
  161. url_list = [
  162. 'https://www.kuaidaili.com/free/inha/',
  163. 'https://www.kuaidaili.com/free/intr/'
  164. ]
  165. for url in url_list:
  166. tree = getHtmlTree(url)
  167. proxy_list = tree.xpath('.//table//tr')
  168. for tr in proxy_list[1:]:
  169. yield ':'.join(tr.xpath('./td/text()')[0:2])
  170. @staticmethod
  171. def freeProxyEight():
  172. """
  173. 秘密代理 http://www.mimiip.com 已停用
  174. """
  175. url_gngao = ['http://www.mimiip.com/gngao/%s' % n for n in range(1, 2)] # 国内高匿
  176. url_gnpu = ['http://www.mimiip.com/gnpu/%s' % n for n in range(1, 2)] # 国内普匿
  177. url_gntou = ['http://www.mimiip.com/gntou/%s' % n for n in range(1, 2)] # 国内透明
  178. url_list = url_gngao + url_gnpu + url_gntou
  179. request = WebRequest()
  180. for url in url_list:
  181. r = request.get(url, timeout=10)
  182. 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)
  183. for proxy in proxies:
  184. yield ':'.join(proxy)
  185. @staticmethod
  186. def freeProxyNinth():
  187. """
  188. 码农代理 https://proxy.coderbusy.com/ 已停用
  189. :return:
  190. """
  191. urls = ['https://proxy.coderbusy.com/classical/country/cn.aspx?page=1']
  192. request = WebRequest()
  193. for url in urls:
  194. r = request.get(url, timeout=10)
  195. proxies = re.findall('data-ip="(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})".+?>(\d+)</td>', r.text)
  196. for proxy in proxies:
  197. yield ':'.join(proxy)
  198. @staticmethod
  199. def freeProxyTen():
  200. """
  201. 云代理 http://www.ip3366.net/free/
  202. :return:
  203. """
  204. urls = ['http://www.ip3366.net/free/']
  205. request = WebRequest()
  206. for url in urls:
  207. r = request.get(url, timeout=10)
  208. 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)
  209. for proxy in proxies:
  210. yield ":".join(proxy)
  211. @staticmethod
  212. def freeProxyEleven():
  213. """
  214. IP海 http://www.iphai.com/free/ng
  215. :return:
  216. """
  217. urls = [
  218. 'http://www.iphai.com/free/ng',
  219. 'http://www.iphai.com/free/np',
  220. 'http://www.iphai.com/free/wg',
  221. 'http://www.iphai.com/free/wp'
  222. ]
  223. request = WebRequest()
  224. for url in urls:
  225. r = request.get(url, timeout=10)
  226. 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>',
  227. r.text)
  228. for proxy in proxies:
  229. yield ":".join(proxy)
  230. @staticmethod
  231. def freeProxyTwelve(page_count=2):
  232. """
  233. http://ip.jiangxianli.com/?page=
  234. 免费代理库
  235. 超多量
  236. :return:
  237. """
  238. for i in range(1, page_count + 1):
  239. url = 'http://ip.jiangxianli.com/?page={}'.format(i)
  240. html_tree = getHtmlTree(url)
  241. tr_list = html_tree.xpath("/html/body/div[1]/div/div[1]/div[2]/table/tbody/tr")
  242. if len(tr_list) == 0:
  243. continue
  244. for tr in tr_list:
  245. yield tr.xpath("./td[2]/text()")[0] + ":" + tr.xpath("./td[3]/text()")[0]
  246. @staticmethod
  247. def freeProxyWallFirst():
  248. """
  249. 墙外网站 cn-proxy
  250. :return:
  251. """
  252. urls = ['http://cn-proxy.com/', 'http://cn-proxy.com/archives/218']
  253. request = WebRequest()
  254. for url in urls:
  255. r = request.get(url, timeout=10)
  256. 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)
  257. for proxy in proxies:
  258. yield ':'.join(proxy)
  259. @staticmethod
  260. def freeProxyWallSecond():
  261. """
  262. https://proxy-list.org/english/index.php
  263. :return:
  264. """
  265. urls = ['https://proxy-list.org/english/index.php?p=%s' % n for n in range(1, 10)]
  266. request = WebRequest()
  267. import base64
  268. for url in urls:
  269. r = request.get(url, timeout=10)
  270. proxies = re.findall(r"Proxy\('(.*?)'\)", r.text)
  271. for proxy in proxies:
  272. yield base64.b64decode(proxy).decode()
  273. @staticmethod
  274. def freeProxyWallThird():
  275. urls = ['https://list.proxylistplus.com/Fresh-HTTP-Proxy-List-1']
  276. request = WebRequest()
  277. for url in urls:
  278. r = request.get(url, timeout=10)
  279. 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)
  280. for proxy in proxies:
  281. yield ':'.join(proxy)
  282. if __name__ == '__main__':
  283. from CheckProxy import CheckProxy
  284. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy01())
  285. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy02)
  286. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy03)
  287. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy04)
  288. CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxySixth)
  289. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxySeventh)
  290. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxyEight)
  291. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxyNinth)
  292. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxyTen)
  293. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxyEleven)
  294. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxyTwelve)
  295. # CheckProxy.checkAllGetProxyFunc()