getFreeProxy.py 11 KB

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