getFreeProxy.py 11 KB

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