proxyFetcher.py 12 KB

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