getFreeProxy.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. ]
  37. for url in url_list:
  38. html_tree = getHtmlTree(url)
  39. ul_list = html_tree.xpath('//ul[@class="l2"]')
  40. for ul in ul_list:
  41. try:
  42. yield ':'.join(ul.xpath('.//li/text()')[0:2])
  43. except Exception as e:
  44. print(e)
  45. @staticmethod
  46. def freeProxy02(count=20):
  47. """
  48. 代理66 http://www.66ip.cn/
  49. :param count: 提取数量
  50. :return:
  51. """
  52. urls = [
  53. "http://www.66ip.cn/mo.php?sxb=&tqsl={}&port=&export=&ktip=&sxa=&submit=%CC%E1++%C8%A1&textarea=",
  54. "http://www.66ip.cn/nmtq.php?getnum={}&isp=0&anonymoustype=0&s"
  55. "tart=&ports=&export=&ipaddress=&area=0&proxytype=2&api=66ip"
  56. ]
  57. try:
  58. import execjs
  59. import requests
  60. headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0',
  61. 'Accept': '*/*',
  62. 'Connection': 'keep-alive',
  63. 'Accept-Language': 'zh-CN,zh;q=0.8'}
  64. session = requests.session()
  65. src = session.get("http://www.66ip.cn/", headers=headers).text
  66. src = src.split("</script>")[0] + '}'
  67. src = src.replace("<script>", "function test() {")
  68. src = src.replace("while(z++)try{eval(", ';var num=10;while(z++)try{var tmp=')
  69. src = src.replace(");break}", ";num--;if(tmp.search('cookie') != -1 | num<0){return tmp}}")
  70. ctx = execjs.compile(src)
  71. src = ctx.call("test")
  72. src = src[src.find("document.cookie="): src.find("};if((")]
  73. src = src.replace("document.cookie=", "")
  74. src = "function test() {var window={}; return %s }" % src
  75. cookie = execjs.compile(src).call('test')
  76. js_cookie = cookie.split(";")[0].split("=")[-1]
  77. except Exception as e:
  78. print(e)
  79. return
  80. for url in urls:
  81. try:
  82. html = session.get(url.format(count), cookies={"__jsl_clearance": js_cookie}, headers=headers).text
  83. ips = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}", html)
  84. for ip in ips:
  85. yield ip.strip()
  86. except Exception as e:
  87. print(e)
  88. pass
  89. @staticmethod
  90. def freeProxy03(page_count=1):
  91. """
  92. 西刺代理 http://www.xicidaili.com
  93. :return:
  94. """
  95. url_list = [
  96. 'http://www.xicidaili.com/nn/', # 高匿
  97. 'http://www.xicidaili.com/nt/', # 透明
  98. ]
  99. for each_url in url_list:
  100. for i in range(1, page_count + 1):
  101. page_url = each_url + str(i)
  102. tree = getHtmlTree(page_url)
  103. proxy_list = tree.xpath('.//table[@id="ip_list"]//tr[position()>1]')
  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 freeProxy04():
  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. # HTML中的port是随机数,真正的端口编码在class后面的字母中。
  130. # 比如这个:
  131. # <span class="port CFACE">9054</span>
  132. # CFACE解码后对应的是3128。
  133. port = 0
  134. for _ in each_proxy.xpath(".//span[contains(@class, 'port')]"
  135. "/attribute::class")[0]. \
  136. replace("port ", ""):
  137. port *= 10
  138. port += (ord(_) - ord('A'))
  139. port /= 8
  140. yield '{}:{}'.format(ip_addr, int(port))
  141. except Exception as e:
  142. pass
  143. @staticmethod
  144. def freeProxy05():
  145. """
  146. 快代理 https://www.kuaidaili.com
  147. """
  148. url_list = [
  149. 'https://www.kuaidaili.com/free/inha/',
  150. 'https://www.kuaidaili.com/free/intr/'
  151. ]
  152. for url in url_list:
  153. tree = getHtmlTree(url)
  154. proxy_list = tree.xpath('.//table//tr')
  155. sleep(1) # 必须sleep 不然第二条请求不到数据
  156. for tr in proxy_list[1:]:
  157. yield ':'.join(tr.xpath('./td/text()')[0:2])
  158. @staticmethod
  159. def freeProxy06():
  160. """
  161. 码农代理 https://proxy.coderbusy.com/
  162. :return:
  163. """
  164. urls = ['https://proxy.coderbusy.com/']
  165. for url in urls:
  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 freeProxy07():
  172. """
  173. 云代理 http://www.ip3366.net/free/
  174. :return:
  175. """
  176. urls = ['http://www.ip3366.net/free/?stype=1',
  177. "http://www.ip3366.net/free/?stype=2"]
  178. request = WebRequest()
  179. for url in urls:
  180. r = request.get(url, timeout=10)
  181. 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)
  182. for proxy in proxies:
  183. yield ":".join(proxy)
  184. @staticmethod
  185. def freeProxy08():
  186. """
  187. IP海 http://www.iphai.com/free/ng
  188. :return:
  189. """
  190. urls = [
  191. 'http://www.iphai.com/free/ng',
  192. 'http://www.iphai.com/free/np',
  193. 'http://www.iphai.com/free/wg',
  194. 'http://www.iphai.com/free/wp'
  195. ]
  196. request = WebRequest()
  197. for url in urls:
  198. r = request.get(url, timeout=10)
  199. 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>',
  200. r.text)
  201. for proxy in proxies:
  202. yield ":".join(proxy)
  203. @staticmethod
  204. def freeProxy09(page_count=2):
  205. """
  206. http://ip.jiangxianli.com/?page=
  207. 免费代理库
  208. :return:
  209. """
  210. for i in range(1, page_count + 1):
  211. url = 'http://ip.jiangxianli.com/?page={}'.format(i)
  212. html_tree = getHtmlTree(url)
  213. tr_list = html_tree.xpath("/html/body/div[1]/div/div[1]/div[2]/table/tbody/tr")
  214. if len(tr_list) == 0:
  215. continue
  216. for tr in tr_list:
  217. yield tr.xpath("./td[2]/text()")[0] + ":" + tr.xpath("./td[3]/text()")[0]
  218. # @staticmethod
  219. # def freeProxy10():
  220. # """
  221. # 墙外网站 cn-proxy
  222. # :return:
  223. # """
  224. # urls = ['http://cn-proxy.com/', 'http://cn-proxy.com/archives/218']
  225. # request = WebRequest()
  226. # for url in urls:
  227. # r = request.get(url, timeout=10)
  228. # 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)
  229. # for proxy in proxies:
  230. # yield ':'.join(proxy)
  231. # @staticmethod
  232. # def freeProxy11():
  233. # """
  234. # https://proxy-list.org/english/index.php
  235. # :return:
  236. # """
  237. # urls = ['https://proxy-list.org/english/index.php?p=%s' % n for n in range(1, 10)]
  238. # request = WebRequest()
  239. # import base64
  240. # for url in urls:
  241. # r = request.get(url, timeout=10)
  242. # proxies = re.findall(r"Proxy\('(.*?)'\)", r.text)
  243. # for proxy in proxies:
  244. # yield base64.b64decode(proxy).decode()
  245. # @staticmethod
  246. # def freeProxy12():
  247. # urls = ['https://list.proxylistplus.com/Fresh-HTTP-Proxy-List-1']
  248. # request = WebRequest()
  249. # for url in urls:
  250. # r = request.get(url, timeout=10)
  251. # 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)
  252. # for proxy in proxies:
  253. # yield ':'.join(proxy)
  254. if __name__ == '__main__':
  255. from CheckProxy import CheckProxy
  256. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy01())
  257. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy02)
  258. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy03)
  259. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy04)
  260. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy05)
  261. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy06)
  262. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy07)
  263. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy08)
  264. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy09)
  265. CheckProxy.checkAllGetProxyFunc()