getFreeProxy.py 13 KB

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