proxyFetcher.py 11 KB

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