getFreeProxy.py 11 KB

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