getFreeProxy.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. from time import sleep
  18. sys.path.append('..')
  19. from Util.WebRequest import WebRequest
  20. from Util.utilFunction import getHtmlTree
  21. # for debug to disable insecureWarning
  22. requests.packages.urllib3.disable_warnings()
  23. class GetFreeProxy(object):
  24. """
  25. proxy getter
  26. """
  27. @staticmethod
  28. def freeProxy01():
  29. """
  30. 无忧代理 http://www.data5u.com/
  31. 几乎没有能用的
  32. :return:
  33. """
  34. url_list = [
  35. 'http://www.data5u.com/',
  36. 'http://www.data5u.com/free/gngn/index.shtml',
  37. 'http://www.data5u.com/free/gnpt/index.shtml'
  38. ]
  39. key = 'ABCDEFGHIZ'
  40. for url in url_list:
  41. html_tree = getHtmlTree(url)
  42. ul_list = html_tree.xpath('//ul[@class="l2"]')
  43. for ul in ul_list:
  44. try:
  45. ip = ul.xpath('./span[1]/li/text()')[0]
  46. classnames = ul.xpath('./span[2]/li/attribute::class')[0]
  47. classname = classnames.split(' ')[1]
  48. port_sum = 0
  49. for c in classname:
  50. port_sum *= 10
  51. port_sum += key.index(c)
  52. port = port_sum >> 3
  53. yield '{}:{}'.format(ip, port)
  54. except Exception as e:
  55. print(e)
  56. @staticmethod
  57. def freeProxy02(count=20):
  58. """
  59. 代理66 http://www.66ip.cn/
  60. :param count: 提取数量
  61. :return:
  62. """
  63. urls = [
  64. "http://www.66ip.cn/mo.php?sxb=&tqsl={}&port=&export=&ktip=&sxa=&submit=%CC%E1++%C8%A1&textarea=",
  65. "http://www.66ip.cn/nmtq.php?getnum={}&isp=0&anonymoustype=0&s"
  66. "tart=&ports=&export=&ipaddress=&area=0&proxytype=2&api=66ip"
  67. ]
  68. try:
  69. import execjs
  70. import requests
  71. headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0',
  72. 'Accept': '*/*',
  73. 'Connection': 'keep-alive',
  74. 'Accept-Language': 'zh-CN,zh;q=0.8'}
  75. session = requests.session()
  76. src = session.get("http://www.66ip.cn/", headers=headers).text
  77. src = src.split("</script>")[0] + '}'
  78. src = src.replace("<script>", "function test() {")
  79. src = src.replace("while(z++)try{eval(", ';var num=10;while(z++)try{var tmp=')
  80. src = src.replace(");break}", ";num--;if(tmp.search('cookie') != -1 | num<0){return tmp}}")
  81. ctx = execjs.compile(src)
  82. src = ctx.call("test")
  83. src = src[src.find("document.cookie="): src.find("};if((")]
  84. src = src.replace("document.cookie=", "")
  85. src = "function test() {var window={}; return %s }" % src
  86. cookie = execjs.compile(src).call('test')
  87. js_cookie = cookie.split(";")[0].split("=")[-1]
  88. except Exception as e:
  89. print(e)
  90. return
  91. for url in urls:
  92. try:
  93. html = session.get(url.format(count), cookies={"__jsl_clearance": js_cookie}, headers=headers).text
  94. ips = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}", html)
  95. for ip in ips:
  96. yield ip.strip()
  97. except Exception as e:
  98. print(e)
  99. pass
  100. @staticmethod
  101. def freeProxy03(page_count=1):
  102. """
  103. 西刺代理 http://www.xicidaili.com
  104. :return:
  105. """
  106. url_list = [
  107. 'http://www.xicidaili.com/nn/', # 高匿
  108. 'http://www.xicidaili.com/nt/', # 透明
  109. ]
  110. for each_url in url_list:
  111. for i in range(1, page_count + 1):
  112. page_url = each_url + str(i)
  113. tree = getHtmlTree(page_url)
  114. proxy_list = tree.xpath('.//table[@id="ip_list"]//tr[position()>1]')
  115. for proxy in proxy_list:
  116. try:
  117. yield ':'.join(proxy.xpath('./td/text()')[0:2])
  118. except Exception as e:
  119. pass
  120. @staticmethod
  121. def freeProxy04():
  122. """
  123. guobanjia http://www.goubanjia.com/
  124. :return:
  125. """
  126. url = "http://www.goubanjia.com/"
  127. tree = getHtmlTree(url)
  128. proxy_list = tree.xpath('//td[@class="ip"]')
  129. # 此网站有隐藏的数字干扰,或抓取到多余的数字或.符号
  130. # 需要过滤掉<p style="display:none;">的内容
  131. xpath_str = """.//*[not(contains(@style, 'display: none'))
  132. and not(contains(@style, 'display:none'))
  133. and not(contains(@class, 'port'))
  134. ]/text()
  135. """
  136. for each_proxy in proxy_list:
  137. try:
  138. # :符号裸放在td下,其他放在div span p中,先分割找出ip,再找port
  139. ip_addr = ''.join(each_proxy.xpath(xpath_str))
  140. # HTML中的port是随机数,真正的端口编码在class后面的字母中。
  141. # 比如这个:
  142. # <span class="port CFACE">9054</span>
  143. # CFACE解码后对应的是3128。
  144. port = 0
  145. for _ in each_proxy.xpath(".//span[contains(@class, 'port')]"
  146. "/attribute::class")[0]. \
  147. replace("port ", ""):
  148. port *= 10
  149. port += (ord(_) - ord('A'))
  150. port /= 8
  151. yield '{}:{}'.format(ip_addr, int(port))
  152. except Exception as e:
  153. pass
  154. @staticmethod
  155. def freeProxy05():
  156. """
  157. 快代理 https://www.kuaidaili.com
  158. """
  159. url_list = [
  160. 'https://www.kuaidaili.com/free/inha/',
  161. 'https://www.kuaidaili.com/free/intr/'
  162. ]
  163. for url in url_list:
  164. tree = getHtmlTree(url)
  165. proxy_list = tree.xpath('.//table//tr')
  166. sleep(1) # 必须sleep 不然第二条请求不到数据
  167. for tr in proxy_list[1:]:
  168. yield ':'.join(tr.xpath('./td/text()')[0:2])
  169. @staticmethod
  170. def freeProxy06():
  171. """
  172. 码农代理 https://proxy.coderbusy.com/
  173. :return:
  174. """
  175. urls = ['https://proxy.coderbusy.com/']
  176. for url in urls:
  177. tree = getHtmlTree(url)
  178. proxy_list = tree.xpath('.//table//tr')
  179. for tr in proxy_list[1:]:
  180. yield ':'.join(tr.xpath('./td/text()')[0:2])
  181. @staticmethod
  182. def freeProxy07():
  183. """
  184. 云代理 http://www.ip3366.net/free/
  185. :return:
  186. """
  187. urls = ['http://www.ip3366.net/free/?stype=1',
  188. "http://www.ip3366.net/free/?stype=2"]
  189. request = WebRequest()
  190. for url in urls:
  191. r = request.get(url, timeout=10)
  192. 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)
  193. for proxy in proxies:
  194. yield ":".join(proxy)
  195. @staticmethod
  196. def freeProxy08():
  197. """
  198. IP海 http://www.iphai.com/free/ng
  199. :return:
  200. """
  201. urls = [
  202. 'http://www.iphai.com/free/ng',
  203. 'http://www.iphai.com/free/np',
  204. 'http://www.iphai.com/free/wg',
  205. 'http://www.iphai.com/free/wp'
  206. ]
  207. request = WebRequest()
  208. for url in urls:
  209. r = request.get(url, timeout=10)
  210. 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>',
  211. r.text)
  212. for proxy in proxies:
  213. yield ":".join(proxy)
  214. @staticmethod
  215. def freeProxy09(page_count=2):
  216. """
  217. http://ip.jiangxianli.com/?page=
  218. 免费代理库
  219. :return:
  220. """
  221. for i in range(1, page_count + 1):
  222. url = 'http://ip.jiangxianli.com/?page={}'.format(i)
  223. html_tree = getHtmlTree(url)
  224. tr_list = html_tree.xpath("/html/body/div[1]/div/div[1]/div[2]/table/tbody/tr")
  225. if len(tr_list) == 0:
  226. continue
  227. for tr in tr_list:
  228. yield tr.xpath("./td[2]/text()")[0] + ":" + tr.xpath("./td[3]/text()")[0]
  229. # @staticmethod
  230. # def freeProxy10():
  231. # """
  232. # 墙外网站 cn-proxy
  233. # :return:
  234. # """
  235. # urls = ['http://cn-proxy.com/', 'http://cn-proxy.com/archives/218']
  236. # request = WebRequest()
  237. # for url in urls:
  238. # r = request.get(url, timeout=10)
  239. # 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)
  240. # for proxy in proxies:
  241. # yield ':'.join(proxy)
  242. # @staticmethod
  243. # def freeProxy11():
  244. # """
  245. # https://proxy-list.org/english/index.php
  246. # :return:
  247. # """
  248. # urls = ['https://proxy-list.org/english/index.php?p=%s' % n for n in range(1, 10)]
  249. # request = WebRequest()
  250. # import base64
  251. # for url in urls:
  252. # r = request.get(url, timeout=10)
  253. # proxies = re.findall(r"Proxy\('(.*?)'\)", r.text)
  254. # for proxy in proxies:
  255. # yield base64.b64decode(proxy).decode()
  256. # @staticmethod
  257. # def freeProxy12():
  258. # urls = ['https://list.proxylistplus.com/Fresh-HTTP-Proxy-List-1']
  259. # request = WebRequest()
  260. # for url in urls:
  261. # r = request.get(url, timeout=10)
  262. # 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)
  263. # for proxy in proxies:
  264. # yield ':'.join(proxy)
  265. if __name__ == '__main__':
  266. from CheckProxy import CheckProxy
  267. CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy01)
  268. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy02)
  269. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy03)
  270. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy04)
  271. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy05)
  272. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy06)
  273. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy07)
  274. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy08)
  275. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy09)
  276. CheckProxy.checkAllGetProxyFunc()