getFreeProxy.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. # for debug to disable insecureWarning
  24. requests.packages.urllib3.disable_warnings()
  25. HEADER = {'Connection': 'keep-alive',
  26. 'Cache-Control': 'max-age=0',
  27. 'Upgrade-Insecure-Requests': '1',
  28. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko)',
  29. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  30. 'Accept-Encoding': 'gzip, deflate, sdch',
  31. 'Accept-Language': 'zh-CN,zh;q=0.8',
  32. }
  33. class GetFreeProxy(object):
  34. """
  35. proxy getter
  36. """
  37. def __init__(self):
  38. pass
  39. @staticmethod
  40. @robustCrawl
  41. def freeProxyFirst(page=10):
  42. """
  43. 抓取快代理IP http://www.kuaidaili.com/
  44. :param page: 翻页数
  45. :return:
  46. """
  47. url_list = ('http://www.kuaidaili.com/proxylist/{page}/'.format(page=page) for page in range(1, page + 1))
  48. # 页数不用太多, 后面的全是历史IP, 可用性不高
  49. for url in url_list:
  50. tree = getHtmlTree(url)
  51. proxy_list = tree.xpath('.//div[@id="index_free_list"]//tbody/tr')
  52. for proxy in proxy_list:
  53. yield ':'.join(proxy.xpath('./td/text()')[0:2])
  54. @staticmethod
  55. @robustCrawl
  56. def freeProxySecond(proxy_number=100):
  57. """
  58. 抓取代理66 http://www.66ip.cn/
  59. :param proxy_number: 代理数量
  60. :return:
  61. """
  62. url = "http://m.66ip.cn/mo.php?sxb=&tqsl={}&port=&export=&ktip=&sxa=&submit=%CC%E1++%C8%A1&textarea=".format(
  63. proxy_number)
  64. html = requests.get(url, headers=HEADER).content
  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. 抓取有代理 http://www.youdaili.net/Daili/http/
  72. :param days:
  73. :return:
  74. """
  75. url = "http://www.youdaili.net/Daili/http/"
  76. tree = getHtmlTree(url)
  77. page_url_list = tree.xpath('.//div[@class="chunlist"]/ul/li/p/a/@href')[0:days]
  78. for page_url in page_url_list:
  79. html = requests.get(page_url, headers=HEADER).content
  80. # print html
  81. proxy_list = re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}', html)
  82. for proxy in proxy_list:
  83. yield proxy
  84. @staticmethod
  85. @robustCrawl
  86. def freeProxyFourth():
  87. """
  88. 抓取西刺代理 http://api.xicidaili.com/free2016.txt
  89. :return:
  90. """
  91. url_list = ['http://www.xicidaili.com/nn', # 高匿
  92. 'http://www.xicidaili.com/nt', # 透明
  93. ]
  94. for each_url in url_list:
  95. tree = getHtmlTree(each_url)
  96. proxy_list = tree.xpath('.//table[@id="ip_list"]//tr')
  97. for proxy in proxy_list:
  98. yield ':'.join(proxy.xpath('./td/text()')[0:2])
  99. @staticmethod
  100. @robustCrawl
  101. def freeProxyFifth():
  102. """
  103. 抓取guobanjia http://www.goubanjia.com/free/gngn/index.shtml
  104. :return:
  105. """
  106. url = "http://www.goubanjia.com/free/gngn/index{page}.shtml"
  107. for page in range(1, 10):
  108. page_url = url.format(page=page)
  109. tree = getHtmlTree(page_url)
  110. proxy_list = tree.xpath('//td[@class="ip"]')
  111. for each_proxy in proxy_list:
  112. yield ''.join(each_proxy.xpath('.//text()'))
  113. if __name__ == '__main__':
  114. gg = GetFreeProxy()
  115. # for e in gg.freeProxyFirst():
  116. # print e
  117. # for e in gg.freeProxySecond():
  118. # print e
  119. # for e in gg.freeProxyThird():
  120. # print e
  121. #
  122. # for e in gg.freeProxyFourth():
  123. # print e
  124. for e in gg.freeProxyFifth():
  125. print(e)