getFreeProxy.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. class GetFreeProxy(object):
  27. """
  28. proxy getter
  29. """
  30. def __init__(self):
  31. pass
  32. @staticmethod
  33. @robustCrawl # decoration print error if exception happen
  34. def freeProxyFirst(page=10):
  35. """
  36. 抓取无忧代理 http://www.data5u.com/
  37. :param page: 页数
  38. :return:
  39. """
  40. url_list = ['http://www.data5u.com/',
  41. 'http://www.data5u.com/free/',
  42. 'http://www.data5u.com/free/gngn/index.shtml',
  43. 'http://www.data5u.com/free/gnpt/index.shtml']
  44. for url in url_list:
  45. html_tree = getHtmlTree(url)
  46. ul_list = html_tree.xpath('//ul[@class="l2"]')
  47. for ul in ul_list:
  48. yield ':'.join(ul.xpath('.//li/text()')[0:2])
  49. @staticmethod
  50. @robustCrawl
  51. def freeProxySecond(proxy_number=100):
  52. """
  53. 抓取代理66 http://www.66ip.cn/
  54. :param proxy_number: 代理数量
  55. :return:
  56. """
  57. url = "http://www.66ip.cn/mo.php?sxb=&tqsl={}&port=&export=&ktip=&sxa=&submit=%CC%E1++%C8%A1&textarea=".format(
  58. proxy_number)
  59. request = WebRequest()
  60. html = request.get(url).content
  61. for proxy in re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}', html):
  62. yield proxy
  63. @staticmethod
  64. @robustCrawl
  65. def freeProxyThird(days=1):
  66. """
  67. 抓取ip181 http://www.ip181.com/
  68. :param days:
  69. :return:
  70. """
  71. url = 'http://www.ip181.com/'
  72. html_tree = getHtmlTree(url)
  73. tr_list = html_tree.xpath('//tr')[1:]
  74. for tr in tr_list:
  75. yield ':'.join(tr.xpath('./td/text()')[0:2])
  76. @staticmethod
  77. @robustCrawl
  78. def freeProxyFourth():
  79. """
  80. 抓取西刺代理 http://api.xicidaili.com/free2016.txt
  81. :return:
  82. """
  83. url_list = ['http://www.xicidaili.com/nn', # 高匿
  84. 'http://www.xicidaili.com/nt', # 透明
  85. ]
  86. for each_url in url_list:
  87. tree = getHtmlTree(each_url)
  88. proxy_list = tree.xpath('.//table[@id="ip_list"]//tr')
  89. for proxy in proxy_list:
  90. yield ':'.join(proxy.xpath('./td/text()')[0:2])
  91. @staticmethod
  92. @robustCrawl
  93. def freeProxyFifth():
  94. """
  95. 抓取guobanjia http://www.goubanjia.com/free/gngn/index.shtml
  96. :return:
  97. """
  98. url = "http://www.goubanjia.com/free/gngn/index{page}.shtml"
  99. for page in range(1, 10):
  100. page_url = url.format(page=page)
  101. tree = getHtmlTree(page_url)
  102. proxy_list = tree.xpath('//td[@class="ip"]')
  103. for each_proxy in proxy_list:
  104. yield ''.join(each_proxy.xpath('.//text()'))
  105. if __name__ == '__main__':
  106. gg = GetFreeProxy()
  107. # for e in gg.freeProxyFirst():
  108. # print e
  109. # for e in gg.freeProxySecond():
  110. # print e
  111. # for e in gg.freeProxyThird():
  112. # print e
  113. for e in gg.freeProxyFourth():
  114. print e
  115. # for e in gg.freeProxyFifth():
  116. # print(e)