proxyFetcher.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. # -*- coding: utf-8 -*-
  2. """
  3. -------------------------------------------------
  4. File Name: proxyFetcher
  5. Description :
  6. Author : JHao
  7. date: 2016/11/25
  8. -------------------------------------------------
  9. Change Activity:
  10. 2016/11/25: proxyFetcher
  11. -------------------------------------------------
  12. """
  13. import base64
  14. import requests
  15. __author__ = 'JHao'
  16. import re
  17. from time import sleep
  18. from util.webRequest import WebRequest
  19. class ProxyFetcher(object):
  20. """
  21. proxy getter
  22. """
  23. @staticmethod
  24. def freeProxy01():
  25. """
  26. 无忧代理 http://www.data5u.com/
  27. 几乎没有能用的
  28. :return:
  29. """
  30. url_list = [
  31. 'http://www.data5u.com/',
  32. 'http://www.data5u.com/free/gngn/index.shtml',
  33. 'http://www.data5u.com/free/gnpt/index.shtml'
  34. ]
  35. key = 'ABCDEFGHIZ'
  36. for url in url_list:
  37. html_tree = WebRequest().get(url).tree
  38. ul_list = html_tree.xpath('//ul[@class="l2"]')
  39. for ul in ul_list:
  40. try:
  41. ip = ul.xpath('./span[1]/li/text()')[0]
  42. classnames = ul.xpath('./span[2]/li/attribute::class')[0]
  43. classname = classnames.split(' ')[1]
  44. port_sum = 0
  45. for c in classname:
  46. port_sum *= 10
  47. port_sum += key.index(c)
  48. port = port_sum >> 3
  49. yield '{}:{}'.format(ip, port)
  50. except Exception as e:
  51. print(e)
  52. @staticmethod
  53. def freeProxy02():
  54. """
  55. 代理66 http://www.66ip.cn/
  56. :return:
  57. """
  58. url = "http://www.66ip.cn/mo.php"
  59. resp = WebRequest().get(url, timeout=10)
  60. proxies = re.findall(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5})', resp.text)
  61. for proxy in proxies:
  62. yield proxy
  63. @staticmethod
  64. def freeProxy03(page_count=1):
  65. """
  66. 西刺代理 http://www.xicidaili.com 网站已关闭
  67. :return:
  68. """
  69. url_list = [
  70. 'http://www.xicidaili.com/nn/', # 高匿
  71. 'http://www.xicidaili.com/nt/', # 透明
  72. ]
  73. for each_url in url_list:
  74. for i in range(1, page_count + 1):
  75. page_url = each_url + str(i)
  76. tree = WebRequest().get(page_url).tree
  77. proxy_list = tree.xpath('.//table[@id="ip_list"]//tr[position()>1]')
  78. for proxy in proxy_list:
  79. try:
  80. yield ':'.join(proxy.xpath('./td/text()')[0:2])
  81. except Exception as e:
  82. pass
  83. @staticmethod
  84. def freeProxy04():
  85. """
  86. 全网代理 http://www.goubanjia.com/
  87. :return:
  88. """
  89. url = "http://www.goubanjia.com/"
  90. tree = WebRequest().get(url).tree
  91. proxy_list = tree.xpath('//td[@class="ip"]')
  92. # 此网站有隐藏的数字干扰,或抓取到多余的数字或.符号
  93. # 需要过滤掉<p style="display:none;">的内容
  94. xpath_str = """.//*[not(contains(@style, 'display: none'))
  95. and not(contains(@style, 'display:none'))
  96. and not(contains(@class, 'port'))
  97. ]/text()
  98. """
  99. # port是class属性值加密得到
  100. def _parse_port(port_element):
  101. port_list = []
  102. for letter in port_element:
  103. port_list.append(str("ABCDEFGHIZ".find(letter)))
  104. _port = "".join(port_list)
  105. return int(_port) >> 0x3
  106. for each_proxy in proxy_list:
  107. try:
  108. ip_addr = ''.join(each_proxy.xpath(xpath_str))
  109. port_str = each_proxy.xpath(".//span[contains(@class, 'port')]/@class")[0].split()[-1]
  110. port = _parse_port(port_str.strip())
  111. yield '{}:{}'.format(ip_addr, int(port))
  112. except Exception:
  113. pass
  114. @staticmethod
  115. def freeProxy05(page_count=1):
  116. """
  117. 快代理 https://www.kuaidaili.com
  118. """
  119. url_pattern = [
  120. 'https://www.kuaidaili.com/free/inha/{}/',
  121. 'https://www.kuaidaili.com/free/intr/{}/'
  122. ]
  123. url_list = []
  124. for page_index in range(1, page_count + 1):
  125. for pattern in url_pattern:
  126. url_list.append(pattern.format(page_index))
  127. for url in url_list:
  128. tree = WebRequest().get(url).tree
  129. proxy_list = tree.xpath('.//table//tr')
  130. sleep(1) # 必须sleep 不然第二条请求不到数据
  131. for tr in proxy_list[1:]:
  132. yield ':'.join(tr.xpath('./td/text()')[0:2])
  133. @staticmethod
  134. def freeProxy06():
  135. """
  136. 代理盒子 https://proxy.coderbusy.com/
  137. :return:
  138. """
  139. urls = ['https://proxy.coderbusy.com/zh-hans/ops/country/cn.html']
  140. for url in urls:
  141. tree = WebRequest().get(url).tree
  142. proxy_list = tree.xpath('.//table//tr')
  143. for tr in proxy_list[1:]:
  144. proxy = '{}:{}'.format("".join(tr.xpath("./td[1]/text()")).strip(),
  145. "".join(tr.xpath("./td[2]//text()")).strip())
  146. if proxy:
  147. yield proxy
  148. @staticmethod
  149. def freeProxy07():
  150. """
  151. 云代理 http://www.ip3366.net/free/
  152. :return:
  153. """
  154. urls = ['http://www.ip3366.net/free/?stype=1',
  155. "http://www.ip3366.net/free/?stype=2"]
  156. for url in urls:
  157. r = WebRequest().get(url, timeout=10)
  158. 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)
  159. for proxy in proxies:
  160. yield ":".join(proxy)
  161. @staticmethod
  162. def freeProxy08():
  163. """
  164. IP海 http://www.iphai.com/free/ng
  165. :return:
  166. """
  167. urls = [
  168. 'http://www.iphai.com/free/ng',
  169. 'http://www.iphai.com/free/np',
  170. 'http://www.iphai.com/free/wg',
  171. 'http://www.iphai.com/free/wp'
  172. ]
  173. for url in urls:
  174. r = WebRequest().get(url, timeout=10)
  175. 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>',
  176. r.text)
  177. for proxy in proxies:
  178. yield ":".join(proxy)
  179. @staticmethod
  180. def freeProxy09(page_count=1):
  181. """
  182. http://ip.jiangxianli.com/?page=
  183. 免费代理库
  184. :return:
  185. """
  186. for i in range(1, page_count + 1):
  187. url = 'http://ip.jiangxianli.com/?country=中国&page={}'.format(i)
  188. html_tree = WebRequest().get(url).tree
  189. for index, tr in enumerate(html_tree.xpath("//table//tr")):
  190. if index == 0:
  191. continue
  192. yield ":".join(tr.xpath("./td/text()")[0:2]).strip()
  193. # @staticmethod
  194. # def freeProxy10():
  195. # """
  196. # 墙外网站 cn-proxy
  197. # :return:
  198. # """
  199. # urls = ['http://cn-proxy.com/', 'http://cn-proxy.com/archives/218']
  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>[\w\W]<td>(\d+)</td>', r.text)
  204. # for proxy in proxies:
  205. # yield ':'.join(proxy)
  206. # @staticmethod
  207. # def freeProxy11():
  208. # """
  209. # https://proxy-list.org/english/index.php
  210. # :return:
  211. # """
  212. # urls = ['https://proxy-list.org/english/index.php?p=%s' % n for n in range(1, 10)]
  213. # request = WebRequest()
  214. # import base64
  215. # for url in urls:
  216. # r = request.get(url, timeout=10)
  217. # proxies = re.findall(r"Proxy\('(.*?)'\)", r.text)
  218. # for proxy in proxies:
  219. # yield base64.b64decode(proxy).decode()
  220. # @staticmethod
  221. # def freeProxy12():
  222. # urls = ['https://list.proxylistplus.com/Fresh-HTTP-Proxy-List-1']
  223. # request = WebRequest()
  224. # for url in urls:
  225. # r = request.get(url, timeout=10)
  226. # 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)
  227. # for proxy in proxies:
  228. # yield ':'.join(proxy)
  229. @staticmethod
  230. def freeProxy13(max_page=2):
  231. """
  232. http://www.89ip.cn/index.html
  233. 89免费代理
  234. :param max_page:
  235. :return:
  236. """
  237. base_url = 'http://www.89ip.cn/index_{}.html'
  238. for page in range(1, max_page + 1):
  239. url = base_url.format(page)
  240. r = WebRequest().get(url, timeout=10)
  241. proxies = re.findall(
  242. r'<td.*?>[\s\S]*?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})[\s\S]*?</td>[\s\S]*?<td.*?>[\s\S]*?(\d+)[\s\S]*?</td>',
  243. r.text)
  244. for proxy in proxies:
  245. yield ':'.join(proxy)
  246. @staticmethod
  247. def freeProxy14():
  248. """
  249. http://www.xiladaili.com/
  250. 西拉代理
  251. :return:
  252. """
  253. urls = ['http://www.xiladaili.com/putong/',
  254. "http://www.xiladaili.com/gaoni/",
  255. "http://www.xiladaili.com/http/",
  256. "http://www.xiladaili.com/https/"]
  257. for url in urls:
  258. r = WebRequest().get(url, timeout=10)
  259. ips = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}", r.text)
  260. for ip in ips:
  261. yield ip.strip()
  262. @staticmethod
  263. def freeProxy15():
  264. urls = [
  265. 'https://www.xroxy.com/proxylist.php?port=&type=All_http&ssl=&country=&latency=&reliability=#table',
  266. 'https://www.xroxy.com/proxylist.php?port=&type=All_http&ssl=&country=&latency=&reliability=&sort=reliability&desc=true&pnum=1#table',
  267. 'https://www.xroxy.com/proxylist.php?port=&type=All_http&ssl=&country=&latency=&reliability=&sort=reliability&desc=true&pnum=2#table',
  268. 'https://www.xroxy.com/proxylist.php?port=&type=All_http&ssl=&country=&latency=&reliability=&sort=reliability&desc=true&pnum=3#table',
  269. 'https://www.xroxy.com/proxylist.php?port=&type=All_http&ssl=&country=&latency=&reliability=&sort=reliability&desc=true&pnum=4#table',
  270. ]
  271. _proxies = {'http': 'http://myhome.97admin.com:34185', 'https': 'http://myhome.97admin.com:34185'}
  272. for url in urls:
  273. r = WebRequest().get(url, timeout=10, proxies=_proxies)
  274. proxies = re.findall(
  275. r'<td.*?>[\s\S]*?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})[\s\S]*?</td>[\s\S]*?<td.*?>[\s\S]*?(\d+)[\s\S]*?</td>',
  276. r.text)
  277. for proxy in proxies:
  278. yield ':'.join(proxy)
  279. @staticmethod
  280. def freeProxy16():
  281. urls = [
  282. 'https://hidemy.name/en/proxy-list/?type=h&anon=4#list',
  283. 'https://hidemy.name/en/proxy-list/?type=h&anon=4&start=64#list',
  284. 'https://hidemy.name/en/proxy-list/?type=h&anon=4&start=128#list',
  285. 'https://hidemy.name/en/proxy-list/?type=h&anon=4&start=192#list',
  286. ]
  287. _proxies = {'http': 'http://myhome.97admin.com:34185', 'https': 'http://myhome.97admin.com:34185'}
  288. for url in urls:
  289. r = WebRequest().get(url, timeout=10, proxies=_proxies)
  290. proxies = re.findall(
  291. r'<td>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})</td><td>(\d+)</td>',
  292. r.text)
  293. for proxy in proxies:
  294. yield ':'.join(proxy)