getFreeProxy.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 requests
  16. try:
  17. from importlib import reload # py3 实际不会实用,只是为了不显示语法错误
  18. except:
  19. import sys # py2
  20. reload(sys)
  21. sys.setdefaultencoding('utf-8')
  22. from Util.utilFunction import robustCrawl, getHtmlTree
  23. from Util.WebRequest import WebRequest
  24. # for debug to disable insecureWarning
  25. requests.packages.urllib3.disable_warnings()
  26. """
  27. 66ip.cn
  28. data5u.com
  29. ip181.com
  30. xicidaili.com
  31. goubanjia.com
  32. xdaili.cn
  33. kuaidaili.com
  34. cn-proxy.com
  35. www.mimiip.com
  36. proxy-list.org
  37. cz88.net
  38. ip181.com
  39. """
  40. class GetFreeProxy(object):
  41. """
  42. proxy getter
  43. """
  44. def __init__(self):
  45. pass
  46. @staticmethod
  47. def freeProxyFirst(page=10):
  48. """
  49. 抓取无忧代理 http://www.data5u.com/
  50. :param page: 页数
  51. :return:
  52. """
  53. url_list = ['http://www.data5u.com/',
  54. 'http://www.data5u.com/free/',
  55. 'http://www.data5u.com/free/gngn/index.shtml',
  56. 'http://www.data5u.com/free/gnpt/index.shtml']
  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. pass
  65. @staticmethod
  66. def freeProxySecond(proxy_number=100):
  67. """
  68. 抓取代理66 http://www.66ip.cn/
  69. :param proxy_number: 代理数量
  70. :return:
  71. """
  72. url = "http://www.66ip.cn/mo.php?sxb=&tqsl={}&port=&export=&ktip=&sxa=&submit=%CC%E1++%C8%A1&textarea=".format(
  73. proxy_number)
  74. request = WebRequest()
  75. # html = request.get(url).content
  76. # content为未解码,text为解码后的字符串
  77. html = request.get(url).text
  78. for proxy in re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}', html):
  79. yield proxy
  80. @staticmethod
  81. def freeProxyThird(days=1):
  82. """
  83. 抓取ip181 http://www.ip181.com/
  84. :param days:
  85. :return:
  86. """
  87. url = 'http://www.ip181.com/'
  88. html_tree = getHtmlTree(url)
  89. try:
  90. tr_list = html_tree.xpath('//tr')[1:]
  91. for tr in tr_list:
  92. yield ':'.join(tr.xpath('./td/text()')[0:2])
  93. except Exception as e:
  94. pass
  95. @staticmethod
  96. def freeProxyFourth():
  97. """
  98. 抓取西刺代理 http://api.xicidaili.com/free2016.txt
  99. :return:
  100. """
  101. url_list = ['http://www.xicidaili.com/nn', # 高匿
  102. 'http://www.xicidaili.com/nt', # 透明
  103. ]
  104. for each_url in url_list:
  105. tree = getHtmlTree(each_url)
  106. proxy_list = tree.xpath('.//table[@id="ip_list"]//tr')
  107. for proxy in proxy_list:
  108. try:
  109. yield ':'.join(proxy.xpath('./td/text()')[0:2])
  110. except Exception as e:
  111. pass
  112. @staticmethod
  113. def freeProxyFifth():
  114. """
  115. 抓取guobanjia http://www.goubanjia.com/free/gngn/index.shtml
  116. :return:
  117. """
  118. url = "http://www.goubanjia.com/free/gngn/index{page}.shtml"
  119. for page in range(1, 10):
  120. page_url = url.format(page=page)
  121. tree = getHtmlTree(page_url)
  122. proxy_list = tree.xpath('//td[@class="ip"]')
  123. # 此网站有隐藏的数字干扰,或抓取到多余的数字或.符号
  124. # 需要过滤掉<p style="display:none;">的内容
  125. xpath_str = """.//*[not(contains(@style, 'display: none'))
  126. and not(contains(@style, 'display:none'))
  127. and not(contains(@class, 'port'))
  128. ]/text()
  129. """
  130. for each_proxy in proxy_list:
  131. try:
  132. # :符号裸放在td下,其他放在div span p中,先分割找出ip,再找port
  133. ip_addr = ''.join(each_proxy.xpath(xpath_str))
  134. port = each_proxy.xpath(
  135. ".//span[contains(@class, 'port')]/text()")[0]
  136. yield '{}:{}'.format(ip_addr, port)
  137. except Exception as e:
  138. pass
  139. @staticmethod
  140. def freeProxySixth():
  141. """
  142. 抓取讯代理免费proxy http://www.xdaili.cn/ipagent/freeip/getFreeIps?page=1&rows=10
  143. :return:
  144. """
  145. url = 'http://www.xdaili.cn/ipagent/freeip/getFreeIps?page=1&rows=10'
  146. request = WebRequest()
  147. try:
  148. res = request.get(url).json()
  149. for row in res['RESULT']['rows']:
  150. yield '{}:{}'.format(row['ip'], row['port'])
  151. except Exception as e:
  152. pass
  153. @staticmethod
  154. def freeProxySeventh():
  155. """
  156. 快代理免费https://www.kuaidaili.com/free/inha/1/
  157. """
  158. url = 'https://www.kuaidaili.com/free/inha/{page}/'
  159. for page in range(1, 10):
  160. page_url = url.format(page=page)
  161. tree = getHtmlTree(page_url)
  162. proxy_list = tree.xpath('.//table//tr')
  163. for tr in proxy_list[1:]:
  164. yield ':'.join(tr.xpath('./td/text()')[0:2])
  165. @staticmethod
  166. def freeProxyEight():
  167. urls = ['http://cn-proxy.com/', 'http://cn-proxy.com/archives/218']
  168. request = WebRequest()
  169. for url in urls:
  170. r = requests.get(url)
  171. proxies = re.findall(
  172. '<td>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})</td>[\w\W]<td>(\d+)</td>', r.content)
  173. for proxy in proxies:
  174. yield ':'.join(proxy)
  175. @staticmethod
  176. def freeProxyNight():
  177. urls = ['http://www.mimiip.com/gngao/%s' % n for n in range(1, 10)]
  178. request = WebRequest()
  179. for url in urls:
  180. r = requests.get(url)
  181. proxies = re.findall(
  182. '<td>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})</td>[\w\W].*<td>(\d+)</td>', r.content)
  183. for proxy in proxies:
  184. yield ':'.join(proxy)
  185. @staticmethod
  186. def freeProxyTenth():
  187. urls = ['https://proxy-list.org/english/index.php?p=%s' %
  188. n for n in range(1, 10)]
  189. request = WebRequest()
  190. import base64
  191. for url in urls:
  192. r = requests.get(url)
  193. proxies = re.findall("Proxy\('(.*?)'\)", r.content)
  194. for proxy in proxies:
  195. yield base64.b64decode(proxy)
  196. @staticmethod
  197. def freeProxyEleventh():
  198. urls = ['http://www.cz88.net/proxy/%s' % m for m in
  199. ['index.shtml'] + ['http_%s.shtml' % n for n in range(2, 11)]]
  200. request = WebRequest()
  201. for url in urls:
  202. r = requests.get(url)
  203. proxies = re.findall(
  204. '(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})</div><div class="port">(\d+)</div>', r.content)
  205. for proxy in proxies:
  206. yield ':'.join(proxy)
  207. @staticmethod
  208. def freeProxy12th():
  209. urls = ['http://www.ip181.com/daili/%s.html' % n for n in range(1, 11)]
  210. request = WebRequest()
  211. for url in urls:
  212. r = requests.get(url)
  213. proxies = re.findall(
  214. '<td>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})</td>[\w\W]*?<td>(\d+)</td>', r.content)
  215. for proxy in proxies:
  216. yield ':'.join(proxy)
  217. if __name__ == '__main__':
  218. gg = GetFreeProxy()
  219. # for e in gg.freeProxyFirst():
  220. # print(e)
  221. #
  222. # for e in gg.freeProxySecond():
  223. # print(e)
  224. #
  225. # for e in gg.freeProxyThird():
  226. # print(e)
  227. # for e in gg.freeProxyFourth():
  228. # print(e)
  229. # for e in gg.freeProxyFifth():
  230. # print(e)
  231. # for e in gg.freeProxySixth():
  232. # print(e)
  233. for e in gg.freeProxySeventh():
  234. print(e)