proxyFetcher.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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/']
  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. yield ':'.join(tr.xpath('./td/text()')[0:2])
  143. @staticmethod
  144. def freeProxy07():
  145. """
  146. 云代理 http://www.ip3366.net/free/
  147. :return:
  148. """
  149. urls = ['http://www.ip3366.net/free/?stype=1',
  150. "http://www.ip3366.net/free/?stype=2"]
  151. for url in urls:
  152. r = WebRequest().get(url, timeout=10)
  153. 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)
  154. for proxy in proxies:
  155. yield ":".join(proxy)
  156. @staticmethod
  157. def freeProxy08():
  158. """
  159. IP海 http://www.iphai.com/free/ng
  160. :return:
  161. """
  162. urls = [
  163. 'http://www.iphai.com/free/ng',
  164. 'http://www.iphai.com/free/np',
  165. 'http://www.iphai.com/free/wg',
  166. 'http://www.iphai.com/free/wp'
  167. ]
  168. for url in urls:
  169. r = WebRequest().get(url, timeout=10)
  170. 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>',
  171. r.text)
  172. for proxy in proxies:
  173. yield ":".join(proxy)
  174. @staticmethod
  175. def freeProxy09(page_count=1):
  176. """
  177. http://ip.jiangxianli.com/?page=
  178. 免费代理库
  179. :return:
  180. """
  181. for i in range(1, page_count + 1):
  182. url = 'http://ip.jiangxianli.com/?country=中国&page={}'.format(i)
  183. html_tree = WebRequest().get(url).tree
  184. for index, tr in enumerate(html_tree.xpath("//table//tr")):
  185. if index == 0:
  186. continue
  187. yield ":".join(tr.xpath("./td/text()")[0:2]).strip()
  188. # @staticmethod
  189. # def freeProxy10():
  190. # """
  191. # 墙外网站 cn-proxy
  192. # :return:
  193. # """
  194. # urls = ['http://cn-proxy.com/', 'http://cn-proxy.com/archives/218']
  195. # request = WebRequest()
  196. # for url in urls:
  197. # r = request.get(url, timeout=10)
  198. # 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)
  199. # for proxy in proxies:
  200. # yield ':'.join(proxy)
  201. # @staticmethod
  202. # def freeProxy11():
  203. # """
  204. # https://proxy-list.org/english/index.php
  205. # :return:
  206. # """
  207. # urls = ['https://proxy-list.org/english/index.php?p=%s' % n for n in range(1, 10)]
  208. # request = WebRequest()
  209. # import base64
  210. # for url in urls:
  211. # r = request.get(url, timeout=10)
  212. # proxies = re.findall(r"Proxy\('(.*?)'\)", r.text)
  213. # for proxy in proxies:
  214. # yield base64.b64decode(proxy).decode()
  215. # @staticmethod
  216. # def freeProxy12():
  217. # urls = ['https://list.proxylistplus.com/Fresh-HTTP-Proxy-List-1']
  218. # request = WebRequest()
  219. # for url in urls:
  220. # r = request.get(url, timeout=10)
  221. # 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)
  222. # for proxy in proxies:
  223. # yield ':'.join(proxy)
  224. @staticmethod
  225. def freeProxy13(max_page=2):
  226. """
  227. http://www.qydaili.com/free/?action=china&page=1
  228. 齐云代理
  229. :param max_page:
  230. :return:
  231. """
  232. base_url = 'http://www.qydaili.com/free/?action=china&page='
  233. for page in range(1, max_page + 1):
  234. url = base_url + str(page)
  235. r = WebRequest().get(url, timeout=10)
  236. proxies = re.findall(
  237. r'<td.*?>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})</td>[\s\S]*?<td.*?>(\d+)</td>',
  238. r.text)
  239. for proxy in proxies:
  240. yield ':'.join(proxy)
  241. @staticmethod
  242. def freeProxy14(max_page=2):
  243. """
  244. http://www.89ip.cn/index.html
  245. 89免费代理
  246. :param max_page:
  247. :return:
  248. """
  249. base_url = 'http://www.89ip.cn/index_{}.html'
  250. for page in range(1, max_page + 1):
  251. url = base_url.format(page)
  252. r = WebRequest().get(url, timeout=10)
  253. proxies = re.findall(
  254. 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>',
  255. r.text)
  256. for proxy in proxies:
  257. yield ':'.join(proxy)
  258. @staticmethod
  259. def freeProxy15():
  260. urls = ['http://www.xiladaili.com/putong/',
  261. "http://www.xiladaili.com/gaoni/",
  262. "http://www.xiladaili.com/http/",
  263. "http://www.xiladaili.com/https/"]
  264. for url in urls:
  265. r = WebRequest().get(url, timeout=10)
  266. ips = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}", r.text)
  267. for ip in ips:
  268. yield ip.strip()