getFreeProxy.py 4.3 KB

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