getFreeProxy.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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=2):
  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. port = each_proxy.xpath(".//span[contains(@class, 'port')]/text()")[0]
  122. yield '{}:{}'.format(ip_addr, port)
  123. except Exception as e:
  124. pass
  125. @staticmethod
  126. def freeProxySixth():
  127. """
  128. 讯代理 http://www.xdaili.cn/
  129. :return:
  130. """
  131. url = 'http://www.xdaili.cn/ipagent/freeip/getFreeIps?page=1&rows=10'
  132. request = WebRequest()
  133. try:
  134. res = request.get(url, timeout=10).json()
  135. for row in res['RESULT']['rows']:
  136. yield '{}:{}'.format(row['ip'], row['port'])
  137. except Exception as e:
  138. pass
  139. @staticmethod
  140. def freeProxySeventh():
  141. """
  142. 快代理 https://www.kuaidaili.com
  143. """
  144. url_list = [
  145. 'https://www.kuaidaili.com/free/inha/{page}/',
  146. 'https://www.kuaidaili.com/free/intr/{page}/'
  147. ]
  148. for url in url_list:
  149. for page in range(1, 2):
  150. page_url = url.format(page=page)
  151. tree = getHtmlTree(page_url)
  152. proxy_list = tree.xpath('.//table//tr')
  153. for tr in proxy_list[1:]:
  154. yield ':'.join(tr.xpath('./td/text()')[0:2])
  155. @staticmethod
  156. def freeProxyEight():
  157. """
  158. 秘密代理 http://www.mimiip.com 不能用
  159. """
  160. url_gngao = ['http://www.mimiip.com/gngao/%s' % n for n in range(1, 2)] # 国内高匿
  161. url_gnpu = ['http://www.mimiip.com/gnpu/%s' % n for n in range(1, 2)] # 国内普匿
  162. url_gntou = ['http://www.mimiip.com/gntou/%s' % n for n in range(1, 2)] # 国内透明
  163. url_list = url_gngao + url_gnpu + url_gntou
  164. request = WebRequest()
  165. for url in url_list:
  166. r = request.get(url, timeout=10)
  167. 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)
  168. for proxy in proxies:
  169. yield ':'.join(proxy)
  170. @staticmethod
  171. def freeProxyNinth():
  172. """
  173. 码农代理 https://proxy.coderbusy.com/ 不能用
  174. :return:
  175. """
  176. urls = ['https://proxy.coderbusy.com/classical/country/cn.aspx?page=1']
  177. request = WebRequest()
  178. for url in urls:
  179. r = request.get(url, timeout=10)
  180. proxies = re.findall('data-ip="(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})".+?>(\d+)</td>', r.text)
  181. for proxy in proxies:
  182. yield ':'.join(proxy)
  183. @staticmethod
  184. def freeProxyTen():
  185. """
  186. 云代理 http://www.ip3366.net/free/
  187. :return:
  188. """
  189. urls = ['http://www.ip3366.net/free/']
  190. request = WebRequest()
  191. for url in urls:
  192. r = request.get(url, timeout=10)
  193. 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)
  194. for proxy in proxies:
  195. yield ":".join(proxy)
  196. @staticmethod
  197. def freeProxyEleven():
  198. """
  199. IP海 http://www.iphai.com/free/ng
  200. :return:
  201. """
  202. urls = [
  203. 'http://www.iphai.com/free/ng',
  204. 'http://www.iphai.com/free/np',
  205. 'http://www.iphai.com/free/wg',
  206. 'http://www.iphai.com/free/wp'
  207. ]
  208. request = WebRequest()
  209. for url in urls:
  210. r = request.get(url, timeout=10)
  211. 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>',
  212. r.text)
  213. for proxy in proxies:
  214. yield ":".join(proxy)
  215. @staticmethod
  216. def freeProxyTwelve(page_count=2):
  217. """
  218. guobanjia http://ip.jiangxianli.com/?page=
  219. 免费代理库
  220. 超多量
  221. :return:
  222. """
  223. for i in range(1, page_count + 1):
  224. url = 'http://ip.jiangxianli.com/?page={}'.format(i)
  225. html_tree = getHtmlTree(url)
  226. tr_list = html_tree.xpath("/html/body/div[1]/div/div[1]/div[2]/table/tbody/tr")
  227. if len(tr_list) == 0:
  228. continue
  229. for tr in tr_list:
  230. yield tr.xpath("./td[2]/text()")[0] + ":" + tr.xpath("./td[3]/text()")[0]
  231. @staticmethod
  232. def freeProxyWallFirst():
  233. """
  234. 墙外网站 cn-proxy
  235. :return:
  236. """
  237. urls = ['http://cn-proxy.com/', 'http://cn-proxy.com/archives/218']
  238. request = WebRequest()
  239. for url in urls:
  240. r = request.get(url, timeout=10)
  241. 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)
  242. for proxy in proxies:
  243. yield ':'.join(proxy)
  244. @staticmethod
  245. def freeProxyWallSecond():
  246. """
  247. https://proxy-list.org/english/index.php
  248. :return:
  249. """
  250. urls = ['https://proxy-list.org/english/index.php?p=%s' % n for n in range(1, 10)]
  251. request = WebRequest()
  252. import base64
  253. for url in urls:
  254. r = request.get(url, timeout=10)
  255. proxies = re.findall(r"Proxy\('(.*?)'\)", r.text)
  256. for proxy in proxies:
  257. yield base64.b64decode(proxy).decode()
  258. @staticmethod
  259. def freeProxyWallThird():
  260. urls = ['https://list.proxylistplus.com/Fresh-HTTP-Proxy-List-1']
  261. request = WebRequest()
  262. for url in urls:
  263. r = request.get(url, timeout=10)
  264. 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)
  265. for proxy in proxies:
  266. yield ':'.join(proxy)
  267. if __name__ == '__main__':
  268. from CheckProxy import CheckProxy
  269. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxyFirst)
  270. CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxySecond)
  271. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxyThird)
  272. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxyFourth)
  273. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxyFifth)
  274. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxySixth)
  275. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxySeventh)
  276. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxyEight)
  277. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxyNinth)
  278. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxyTen)
  279. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxyEleven)
  280. # CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxyTwelve)
  281. # CheckProxy.checkAllGetProxyFunc()