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