getFreeProxy.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. import sys
  17. sys.path.append('../')
  18. try:
  19. from importlib import reload # py3 实际不会实用,只是为了不显示语法错误
  20. except:
  21. import sys # py2
  22. reload(sys)
  23. sys.setdefaultencoding('utf-8')
  24. from Util.utilFunction import robustCrawl, getHtmlTree
  25. from Util.WebRequest import WebRequest
  26. # for debug to disable insecureWarning
  27. requests.packages.urllib3.disable_warnings()
  28. class GetFreeProxy(object):
  29. """
  30. proxy getter
  31. """
  32. def __init__(self):
  33. pass
  34. @staticmethod
  35. @robustCrawl # decoration print error if exception happen
  36. def freeProxyFirst(page=10):
  37. """
  38. 抓取无忧代理 http://www.data5u.com/
  39. :param page: 页数
  40. :return:
  41. """
  42. url_list = ['http://www.data5u.com/',
  43. 'http://www.data5u.com/free/',
  44. 'http://www.data5u.com/free/gngn/index.shtml',
  45. 'http://www.data5u.com/free/gnpt/index.shtml']
  46. for url in url_list:
  47. html_tree = getHtmlTree(url)
  48. ul_list = html_tree.xpath('//ul[@class="l2"]')
  49. for ul in ul_list:
  50. yield ':'.join(ul.xpath('.//li/text()')[0:2])
  51. @staticmethod
  52. @robustCrawl
  53. def freeProxySecond(proxy_number=100):
  54. """
  55. 抓取代理66 http://www.66ip.cn/
  56. :param proxy_number: 代理数量
  57. :return:
  58. """
  59. url = "http://www.66ip.cn/mo.php?sxb=&tqsl={}&port=&export=&ktip=&sxa=&submit=%CC%E1++%C8%A1&textarea=".format(
  60. proxy_number)
  61. request = WebRequest()
  62. # html = request.get(url).content
  63. # content为未解码,text为解码后的字符串
  64. html = request.get(url).text
  65. for proxy in re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}', html):
  66. yield proxy
  67. @staticmethod
  68. @robustCrawl
  69. def freeProxyThird(days=1):
  70. """
  71. 抓取ip181 http://www.ip181.com/
  72. :param days:
  73. :return:
  74. """
  75. url = 'http://www.ip181.com/'
  76. html_tree = getHtmlTree(url)
  77. tr_list = html_tree.xpath('//tr')[1:]
  78. for tr in tr_list:
  79. yield ':'.join(tr.xpath('./td/text()')[0:2])
  80. @staticmethod
  81. @robustCrawl
  82. def freeProxyFourth():
  83. """
  84. 抓取西刺代理 http://api.xicidaili.com/free2016.txt
  85. :return:
  86. """
  87. url_list = ['http://www.xicidaili.com/nn', # 高匿
  88. 'http://www.xicidaili.com/nt', # 透明
  89. ]
  90. for each_url in url_list:
  91. tree = getHtmlTree(each_url)
  92. proxy_list = tree.xpath('.//table[@id="ip_list"]//tr')
  93. for proxy in proxy_list:
  94. yield ':'.join(proxy.xpath('./td/text()')[0:2])
  95. @staticmethod
  96. @robustCrawl
  97. def freeProxyFifth():
  98. """
  99. 抓取guobanjia http://www.goubanjia.com/free/gngn/index.shtml
  100. :return:
  101. """
  102. url = "http://www.goubanjia.com/free/gngn/index{page}.shtml"
  103. for page in range(1, 10):
  104. page_url = url.format(page=page)
  105. tree = getHtmlTree(page_url)
  106. proxy_list = tree.xpath('//td[@class="ip"]')
  107. # 此网站有隐藏的数字干扰,或抓取到多余的数字或.符号
  108. # 需要过滤掉<p style="display:none;">的内容
  109. xpath_str = """.//*[not(contains(@style, 'display: none'))
  110. and not(contains(@style, 'display:none'))
  111. and not(contains(@class, 'port'))
  112. ]/text()
  113. """
  114. for each_proxy in proxy_list:
  115. # :符号裸放在td下,其他放在div span p中,先分割找出ip,再找port
  116. ip_addr = ''.join(each_proxy.xpath(xpath_str))
  117. port = each_proxy.xpath(".//span[contains(@class, 'port')]/text()")[0]
  118. yield '{}:{}'.format(ip_addr, port)
  119. @staticmethod
  120. @robustCrawl
  121. def freeProxySixth():
  122. """
  123. 抓取讯代理免费proxy http://www.xdaili.cn/ipagent/freeip/getFreeIps?page=1&rows=10
  124. :return:
  125. """
  126. url = 'http://www.xdaili.cn/ipagent/freeip/getFreeIps?page=1&rows=10'
  127. request = WebRequest()
  128. try:
  129. res = request.get(url).json()
  130. for row in res['RESULT']['rows']:
  131. yield '{}:{}'.format(row['ip'], row['port'])
  132. except Exception as e:
  133. pass
  134. if __name__ == '__main__':
  135. gg = GetFreeProxy()
  136. # for e in gg.freeProxyFirst():
  137. # print e
  138. # for e in gg.freeProxySecond():
  139. # print e
  140. # for e in gg.freeProxyThird():
  141. # print e
  142. for e in gg.freeProxySixth():
  143. print(e)
  144. # for e in gg.freeProxyFifth():
  145. # print(e)