proxyFetcher.py 9.5 KB

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