getFreeProxy.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. print url
  48. tree = getHtmlTree(url)
  49. proxy_list = tree.xpath('.//div[@id="index_free_list"]//tbody/tr')
  50. for proxy in proxy_list:
  51. yield ':'.join(proxy.xpath('./td/text()')[0:2])
  52. @staticmethod
  53. @robustCrawl
  54. def freeProxySecond(proxy_number=100):
  55. """
  56. 抓取代理66 http://www.66ip.cn/
  57. :param proxy_number: 代理数量
  58. :return:
  59. """
  60. url = "http://m.66ip.cn/mo.php?sxb=&tqsl={}&port=&export=&ktip=&sxa=&submit=%CC%E1++%C8%A1&textarea=".format(
  61. proxy_number)
  62. html = requests.get(url, headers=HEADER).content
  63. for proxy in re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}', html):
  64. yield proxy
  65. @staticmethod
  66. @robustCrawl
  67. def freeProxyThird(days=1):
  68. """
  69. 抓取有代理 http://www.youdaili.net/Daili/http/
  70. :param days:
  71. :return:
  72. """
  73. url = "http://www.youdaili.net/Daili/http/"
  74. tree = getHtmlTree(url)
  75. page_url_list = tree.xpath('.//div[@class="chunlist"]/ul/li/p/a/@href')[0:days]
  76. for page_url in page_url_list:
  77. html = requests.get(page_url, headers=HEADER).content
  78. # print html
  79. proxy_list = re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}', html)
  80. for proxy in proxy_list:
  81. yield proxy
  82. @staticmethod
  83. @robustCrawl
  84. def freeProxyFourth():
  85. """
  86. 抓取西刺代理 http://api.xicidaili.com/free2016.txt
  87. :return:
  88. """
  89. url_list = ['http://www.xicidaili.com/nn', # 高匿
  90. 'http://www.xicidaili.com/nt', # 透明
  91. ]
  92. for each_url in url_list:
  93. tree = getHtmlTree(each_url)
  94. proxy_list = tree.xpath('.//table[@id="ip_list"]//tr')
  95. for proxy in proxy_list:
  96. yield ':'.join(proxy.xpath('./td/text()')[0:2])
  97. @staticmethod
  98. @robustCrawl
  99. def freeProxyFifth():
  100. """
  101. 抓取guobanjia http://www.goubanjia.com/free/gngn/index.shtml
  102. :return:
  103. """
  104. url = "http://www.goubanjia.com/free/gngn/index{page}.shtml"
  105. for page in range(1, 10):
  106. page_url = url.format(page=page)
  107. tree = getHtmlTree(page_url)
  108. proxy_list = tree.xpath('//td[@class="ip"]')
  109. for each_proxy in proxy_list:
  110. yield ''.join(each_proxy.xpath('.//text()'))
  111. if __name__ == '__main__':
  112. gg = GetFreeProxy()
  113. # for e in gg.freeProxyFirst():
  114. # print e
  115. # for e in gg.freeProxySecond():
  116. # print e
  117. # for e in gg.freeProxyThird():
  118. # print e
  119. #
  120. # for e in gg.freeProxyFourth():
  121. # print e
  122. for e in gg.freeProxyFifth():
  123. print e