getFreeProxy.py 4.5 KB

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