getFreeProxy.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. # for debug to disable insecureWarning
  26. requests.packages.urllib3.disable_warnings()
  27. """
  28. 66ip.cn
  29. data5u.com
  30. xicidaili.com
  31. goubanjia.com
  32. xdaili.cn
  33. kuaidaili.com
  34. cn-proxy.com
  35. proxy-list.org
  36. www.mimiip.com to do
  37. """
  38. class GetFreeProxy(object):
  39. """
  40. proxy getter
  41. """
  42. def __init__(self):
  43. pass
  44. @staticmethod
  45. def freeProxyFirst(page=10):
  46. """
  47. 无忧代理 http://www.data5u.com/
  48. 几乎没有能用的
  49. :param page: 页数
  50. :return:
  51. """
  52. url_list = [
  53. 'http://www.data5u.com/',
  54. 'http://www.data5u.com/free/gngn/index.shtml',
  55. 'http://www.data5u.com/free/gnpt/index.shtml'
  56. ]
  57. for url in url_list:
  58. html_tree = getHtmlTree(url)
  59. ul_list = html_tree.xpath('//ul[@class="l2"]')
  60. for ul in ul_list:
  61. try:
  62. yield ':'.join(ul.xpath('.//li/text()')[0:2])
  63. except Exception as e:
  64. print(e)
  65. @staticmethod
  66. def freeProxySecond(area=33, page=1):
  67. """
  68. 代理66 http://www.66ip.cn/
  69. :param area: 抓取代理页数,page=1北京代理页,page=2上海代理页......
  70. :param page: 翻页
  71. :return:
  72. """
  73. area = 33 if area > 33 else area
  74. for area_index in range(1, area + 1):
  75. for i in range(1, page + 1):
  76. url = "http://www.66ip.cn/areaindex_{}/{}.html".format(area_index, i)
  77. html_tree = getHtmlTree(url)
  78. tr_list = html_tree.xpath("//*[@id='footer']/div/table/tr[position()>1]")
  79. if len(tr_list) == 0:
  80. continue
  81. for tr in tr_list:
  82. yield tr.xpath("./td[1]/text()")[0] + ":" + tr.xpath("./td[2]/text()")[0]
  83. break
  84. @staticmethod
  85. def freeProxyThird(days=1):
  86. """
  87. ip181 http://www.ip181.com/ 不能用了
  88. :param days:
  89. :return:
  90. """
  91. url = 'http://www.ip181.com/'
  92. html_tree = getHtmlTree(url)
  93. try:
  94. tr_list = html_tree.xpath('//tr')[1:]
  95. for tr in tr_list:
  96. yield ':'.join(tr.xpath('./td/text()')[0:2])
  97. except Exception as e:
  98. pass
  99. @staticmethod
  100. def freeProxyFourth(page_count=2):
  101. """
  102. 西刺代理 http://www.xicidaili.com
  103. :return:
  104. """
  105. url_list = [
  106. 'http://www.xicidaili.com/nn/', # 高匿
  107. 'http://www.xicidaili.com/nt/', # 透明
  108. ]
  109. for each_url in url_list:
  110. for i in range(1, page_count + 1):
  111. page_url = each_url + str(i)
  112. tree = getHtmlTree(page_url)
  113. proxy_list = tree.xpath('.//table[@id="ip_list"]//tr[position()>1]')
  114. for proxy in proxy_list:
  115. try:
  116. yield ':'.join(proxy.xpath('./td/text()')[0:2])
  117. except Exception as e:
  118. pass
  119. @staticmethod
  120. def freeProxyFifth():
  121. """
  122. guobanjia http://www.goubanjia.com/
  123. :return:
  124. """
  125. url = "http://www.goubanjia.com/"
  126. tree = getHtmlTree(url)
  127. proxy_list = tree.xpath('//td[@class="ip"]')
  128. # 此网站有隐藏的数字干扰,或抓取到多余的数字或.符号
  129. # 需要过滤掉<p style="display:none;">的内容
  130. xpath_str = """.//*[not(contains(@style, 'display: none'))
  131. and not(contains(@style, 'display:none'))
  132. and not(contains(@class, 'port'))
  133. ]/text()
  134. """
  135. for each_proxy in proxy_list:
  136. try:
  137. # :符号裸放在td下,其他放在div span p中,先分割找出ip,再找port
  138. ip_addr = ''.join(each_proxy.xpath(xpath_str))
  139. port = each_proxy.xpath(".//span[contains(@class, 'port')]/text()")[0]
  140. yield '{}:{}'.format(ip_addr, port)
  141. except Exception as e:
  142. pass
  143. @staticmethod
  144. def freeProxySixth():
  145. """
  146. 讯代理 http://www.xdaili.cn/
  147. :return:
  148. """
  149. url = 'http://www.xdaili.cn/ipagent/freeip/getFreeIps?page=1&rows=10'
  150. request = WebRequest()
  151. try:
  152. res = request.get(url).json()
  153. for row in res['RESULT']['rows']:
  154. yield '{}:{}'.format(row['ip'], row['port'])
  155. except Exception as e:
  156. pass
  157. @staticmethod
  158. def freeProxySeventh():
  159. """
  160. 快代理 https://www.kuaidaili.com
  161. """
  162. url_list = [
  163. 'https://www.kuaidaili.com/free/inha/{page}/',
  164. 'https://www.kuaidaili.com/free/intr/{page}/'
  165. ]
  166. for url in url_list:
  167. for page in range(1, 5):
  168. page_url = url.format(page=page)
  169. tree = getHtmlTree(page_url)
  170. proxy_list = tree.xpath('.//table//tr')
  171. for tr in proxy_list[1:]:
  172. yield ':'.join(tr.xpath('./td/text()')[0:2])
  173. @staticmethod
  174. def freeProxyEight():
  175. """
  176. 秘密代理 http://www.mimiip.com
  177. """
  178. url_gngao = ['http://www.mimiip.com/gngao/%s' % n for n in range(1, 10)] # 国内高匿
  179. url_gnpu = ['http://www.mimiip.com/gnpu/%s' % n for n in range(1, 10)] # 国内普匿
  180. url_gntou = ['http://www.mimiip.com/gntou/%s' % n for n in range(1, 10)] # 国内透明
  181. url_list = url_gngao + url_gnpu + url_gntou
  182. request = WebRequest()
  183. for url in url_list:
  184. r = request.get(url, use_proxy=True)
  185. 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)
  186. for proxy in proxies:
  187. yield ':'.join(proxy)
  188. @staticmethod
  189. def freeProxyNinth():
  190. """
  191. 码农代理 https://proxy.coderbusy.com/
  192. :return:
  193. """
  194. urls = ['https://proxy.coderbusy.com/classical/country/cn.aspx?page=1']
  195. request = WebRequest()
  196. for url in urls:
  197. r = request.get(url)
  198. proxies = re.findall('data-ip="(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})".+?>(\d+)</td>', r.text)
  199. for proxy in proxies:
  200. yield ':'.join(proxy)
  201. @staticmethod
  202. def freeProxyTen():
  203. """
  204. 云代理 http://www.ip3366.net/free/
  205. :return:
  206. """
  207. urls = ['http://www.ip3366.net/free/']
  208. request = WebRequest()
  209. for url in urls:
  210. r = request.get(url)
  211. 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)
  212. for proxy in proxies:
  213. yield ":".join(proxy)
  214. @staticmethod
  215. def freeProxyEleven():
  216. """
  217. IP海 http://www.iphai.com/free/ng
  218. :return:
  219. """
  220. urls = [
  221. 'http://www.iphai.com/free/ng',
  222. 'http://www.iphai.com/free/np',
  223. 'http://www.iphai.com/free/wg',
  224. 'http://www.iphai.com/free/wp'
  225. ]
  226. request = WebRequest()
  227. for url in urls:
  228. r = request.get(url)
  229. 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>',
  230. r.text)
  231. for proxy in proxies:
  232. yield ":".join(proxy)
  233. @staticmethod
  234. def freeProxyWallFirst():
  235. """
  236. 墙外网站 cn-proxy
  237. :return:
  238. """
  239. urls = ['http://cn-proxy.com/', 'http://cn-proxy.com/archives/218']
  240. request = WebRequest()
  241. for url in urls:
  242. r = request.get(url)
  243. 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)
  244. for proxy in proxies:
  245. yield ':'.join(proxy)
  246. @staticmethod
  247. def freeProxyWallSecond():
  248. """
  249. https://proxy-list.org/english/index.php
  250. :return:
  251. """
  252. urls = ['https://proxy-list.org/english/index.php?p=%s' % n for n in range(1, 10)]
  253. request = WebRequest()
  254. import base64
  255. for url in urls:
  256. r = request.get(url)
  257. proxies = re.findall(r"Proxy\('(.*?)'\)", r.text)
  258. for proxy in proxies:
  259. yield base64.b64decode(proxy).decode()
  260. @staticmethod
  261. def freeProxyWallThird():
  262. urls = ['https://list.proxylistplus.com/Fresh-HTTP-Proxy-List-1']
  263. request = WebRequest()
  264. for url in urls:
  265. r = request.get(url)
  266. 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)
  267. for proxy in proxies:
  268. yield ':'.join(proxy)
  269. if __name__ == '__main__':
  270. gg = GetFreeProxy()
  271. # test_batch(gg.freeProxyFirst())
  272. # test_batch(gg.freeProxySecond())
  273. # test_batch(gg.freeProxyFourth())
  274. # test_batch(gg.freeProxyFifth())
  275. # test_batch(gg.freeProxySixth())
  276. # test_batch(gg.freeProxySeventh())
  277. # test_batch(gg.freeProxyEight())
  278. # test_batch(gg.freeProxyNinth())
  279. # test_batch(gg.freeProxyTen())
  280. # test_batch(gg.freeProxyEleven())
  281. # test_batch(gg.freeProxyWallFirst())
  282. # test_batch(gg.freeProxyWallSecond())
  283. # test_batch(gg.freeProxyWallThird())