getFreeProxy.py 11 KB

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