getFreeProxy.py 10 KB

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