getFreeProxy.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. try:
  18. from importlib import reload # py3 实际不会实用,只是为了不显示语法错误
  19. except:
  20. reload(sys)
  21. sys.setdefaultencoding('utf-8')
  22. sys.path.append('..')
  23. from Util.utilFunction import robustCrawl, getHtmlTree
  24. from Util.WebRequest import WebRequest
  25. from Util.utilFunction import verifyProxyFormat
  26. # for debug to disable insecureWarning
  27. requests.packages.urllib3.disable_warnings()
  28. """
  29. 66ip.cn
  30. data5u.com
  31. xicidaili.com
  32. goubanjia.com
  33. xdaili.cn
  34. kuaidaili.com
  35. cn-proxy.com
  36. proxy-list.org
  37. www.mimiip.com to do
  38. """
  39. class GetFreeProxy(object):
  40. """
  41. proxy getter
  42. """
  43. def __init__(self):
  44. pass
  45. @staticmethod
  46. def freeProxyFirst(page=10):
  47. """
  48. 无忧代理 http://www.data5u.com/
  49. 几乎没有能用的
  50. :param page: 页数
  51. :return:
  52. """
  53. url_list = [
  54. 'http://www.data5u.com/',
  55. 'http://www.data5u.com/free/gngn/index.shtml',
  56. 'http://www.data5u.com/free/gnpt/index.shtml'
  57. ]
  58. for url in url_list:
  59. html_tree = getHtmlTree(url)
  60. ul_list = html_tree.xpath('//ul[@class="l2"]')
  61. for ul in ul_list:
  62. try:
  63. yield ':'.join(ul.xpath('.//li/text()')[0:2])
  64. except Exception as e:
  65. print(e)
  66. @staticmethod
  67. def freeProxySecond(area=33, page=1):
  68. """
  69. 代理66 http://www.66ip.cn/
  70. :param area: 抓取代理页数,page=1北京代理页,page=2上海代理页......
  71. :param page: 翻页
  72. :return:
  73. """
  74. area = 33 if area > 33 else area
  75. for area_index in range(1, area + 1):
  76. for i in range(1, page + 1):
  77. url = "http://www.66ip.cn/areaindex_{}/{}.html".format(area_index, i)
  78. html_tree = getHtmlTree(url)
  79. tr_list = html_tree.xpath("//*[@id='footer']/div/table/tr[position()>1]")
  80. if len(tr_list) == 0:
  81. continue
  82. for tr in tr_list:
  83. yield tr.xpath("./td[1]/text()")[0] + ":" + tr.xpath("./td[2]/text()")[0]
  84. break
  85. @staticmethod
  86. def freeProxyThird(days=1):
  87. """
  88. ip181 http://www.ip181.com/ 不能用了
  89. :param days:
  90. :return:
  91. """
  92. url = 'http://www.ip181.com/'
  93. html_tree = getHtmlTree(url)
  94. try:
  95. tr_list = html_tree.xpath('//tr')[1:]
  96. for tr in tr_list:
  97. yield ':'.join(tr.xpath('./td/text()')[0:2])
  98. except Exception as e:
  99. pass
  100. @staticmethod
  101. def freeProxyFourth(page_count=2):
  102. """
  103. 西刺代理 http://www.xicidaili.com
  104. :return:
  105. """
  106. url_list = [
  107. 'http://www.xicidaili.com/nn/', # 高匿
  108. 'http://www.xicidaili.com/nt/', # 透明
  109. ]
  110. for each_url in url_list:
  111. for i in range(1, page_count + 1):
  112. page_url = each_url + str(i)
  113. tree = getHtmlTree(page_url)
  114. proxy_list = tree.xpath('.//table[@id="ip_list"]//tr[position()>1]')
  115. for proxy in proxy_list:
  116. try:
  117. yield ':'.join(proxy.xpath('./td/text()')[0:2])
  118. except Exception as e:
  119. pass
  120. @staticmethod
  121. def freeProxyFifth():
  122. """
  123. guobanjia http://www.goubanjia.com/
  124. :return:
  125. """
  126. url = "http://www.goubanjia.com/"
  127. tree = getHtmlTree(url)
  128. proxy_list = tree.xpath('//td[@class="ip"]')
  129. # 此网站有隐藏的数字干扰,或抓取到多余的数字或.符号
  130. # 需要过滤掉<p style="display:none;">的内容
  131. xpath_str = """.//*[not(contains(@style, 'display: none'))
  132. and not(contains(@style, 'display:none'))
  133. and not(contains(@class, 'port'))
  134. ]/text()
  135. """
  136. for each_proxy in proxy_list:
  137. try:
  138. # :符号裸放在td下,其他放在div span p中,先分割找出ip,再找port
  139. ip_addr = ''.join(each_proxy.xpath(xpath_str))
  140. port = each_proxy.xpath(".//span[contains(@class, 'port')]/text()")[0]
  141. yield '{}:{}'.format(ip_addr, port)
  142. except Exception as e:
  143. pass
  144. @staticmethod
  145. def freeProxySixth():
  146. """
  147. 讯代理 http://www.xdaili.cn/
  148. :return:
  149. """
  150. url = 'http://www.xdaili.cn/ipagent/freeip/getFreeIps?page=1&rows=10'
  151. request = WebRequest()
  152. try:
  153. res = request.get(url).json()
  154. for row in res['RESULT']['rows']:
  155. yield '{}:{}'.format(row['ip'], row['port'])
  156. except Exception as e:
  157. pass
  158. @staticmethod
  159. def freeProxySeventh():
  160. """
  161. 快代理 https://www.kuaidaili.com
  162. """
  163. url_list = [
  164. 'https://www.kuaidaili.com/free/inha/{page}/',
  165. 'https://www.kuaidaili.com/free/intr/{page}/'
  166. ]
  167. for url in url_list:
  168. for page in range(1, 5):
  169. page_url = url.format(page=page)
  170. tree = getHtmlTree(page_url)
  171. proxy_list = tree.xpath('.//table//tr')
  172. for tr in proxy_list[1:]:
  173. yield ':'.join(tr.xpath('./td/text()')[0:2])
  174. @staticmethod
  175. def freeProxyEight():
  176. """
  177. 秘密代理 http://www.mimiip.com
  178. """
  179. url_gngao = ['http://www.mimiip.com/gngao/%s' % n for n in range(1, 10)] # 国内高匿
  180. url_gnpu = ['http://www.mimiip.com/gnpu/%s' % n for n in range(1, 10)] # 国内普匿
  181. url_gntou = ['http://www.mimiip.com/gntou/%s' % n for n in range(1, 10)] # 国内透明
  182. url_list = url_gngao + url_gnpu + url_gntou
  183. request = WebRequest()
  184. for url in url_list:
  185. r = request.get(url, use_proxy=True)
  186. 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)
  187. for proxy in proxies:
  188. yield ':'.join(proxy)
  189. @staticmethod
  190. def freeProxyNinth():
  191. """
  192. 码农代理 https://proxy.coderbusy.com/
  193. :return:
  194. """
  195. urls = ['https://proxy.coderbusy.com/classical/country/cn.aspx?page=1']
  196. request = WebRequest()
  197. for url in urls:
  198. r = request.get(url)
  199. proxies = re.findall('data-ip="(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})".+?>(\d+)</td>', r.text)
  200. for proxy in proxies:
  201. yield ':'.join(proxy)
  202. @staticmethod
  203. def freeProxyTen():
  204. """
  205. 云代理 http://www.ip3366.net/free/
  206. :return:
  207. """
  208. urls = ['http://www.ip3366.net/free/']
  209. request = WebRequest()
  210. for url in urls:
  211. r = request.get(url)
  212. 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)
  213. for proxy in proxies:
  214. yield ":".join(proxy)
  215. @staticmethod
  216. def freeProxyEleven():
  217. """
  218. IP海 http://www.iphai.com/free/ng
  219. :return:
  220. """
  221. urls = [
  222. 'http://www.iphai.com/free/ng',
  223. 'http://www.iphai.com/free/np',
  224. 'http://www.iphai.com/free/wg',
  225. 'http://www.iphai.com/free/wp'
  226. ]
  227. request = WebRequest()
  228. for url in urls:
  229. r = request.get(url)
  230. 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>',
  231. r.text)
  232. for proxy in proxies:
  233. yield ":".join(proxy)
  234. @staticmethod
  235. def freeProxyTwelve(page_count=8):
  236. """
  237. guobanjia http://ip.jiangxianli.com/?page=
  238. 免费代理库
  239. 超多量
  240. :return:
  241. """
  242. for i in range(1, page_count + 1):
  243. url = 'http://ip.jiangxianli.com/?page={}'.format(i)
  244. # print(url)
  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)
  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)
  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)
  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. gg = GetFreeProxy()
  289. # test_batch(gg.freeProxyFirst())
  290. # test_batch(gg.freeProxySecond())
  291. # test_batch(gg.freeProxyFourth())
  292. # test_batch(gg.freeProxyFifth())
  293. # test_batch(gg.freeProxySixth())
  294. # test_batch(gg.freeProxySeventh())
  295. # test_batch(gg.freeProxyEight())
  296. # test_batch(gg.freeProxyNinth())
  297. # test_batch(gg.freeProxyTen())
  298. # test_batch(gg.freeProxyEleven())
  299. proxy_iter = gg.freeProxyTwelve()
  300. proxy_set = set()
  301. for proxy in proxy_iter:
  302. proxy = proxy.strip()
  303. if proxy and verifyProxyFormat(proxy):
  304. #self.log.info('{func}: fetch proxy {proxy}'.format(func=proxyGetter, proxy=proxy))
  305. proxy_set.add(proxy)
  306. #else:
  307. #self.log.error('{func}: fetch proxy {proxy} error'.format(func=proxyGetter, proxy=proxy))
  308. # store
  309. for proxy in proxy_set:
  310. print(proxy)
  311. # test_batch(gg.freeProxyTwelve())
  312. # test_batch(gg.freeProxyWallFirst())
  313. # test_batch(gg.freeProxyWallSecond())
  314. # test_batch(gg.freeProxyWallThird())