getFreeProxy.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. class GetFreeProxy(object):
  23. """
  24. proxy getter
  25. """
  26. def __init__(self):
  27. pass
  28. @staticmethod
  29. @robustCrawl
  30. def freeProxyFirst(page=10):
  31. """
  32. 抓取快代理IP http://www.kuaidaili.com/
  33. :param page: 翻页数
  34. :return:
  35. """
  36. url_list = ('http://www.kuaidaili.com/proxylist/{page}/'.format(page=page) for page in range(1, page + 1))
  37. # 页数不用太多, 后面的全是历史IP, 可用性不高
  38. header = {
  39. 'Host': 'www.kuaidaili.com',
  40. 'Connection': 'keep-alive',
  41. 'Cache-Control': 'max-age=0',
  42. 'Upgrade-Insecure-Requests': '1',
  43. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
  44. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  45. 'Accept-Encoding': 'gzip, deflate, sdch',
  46. 'Accept-Language': 'zh-CN,zh;q=0.8',
  47. }
  48. for url in url_list:
  49. tree = getHtmlTree(url, header=header)
  50. proxy_list = tree.xpath('.//div[@id="index_free_list"]//tbody/tr')
  51. for proxy in proxy_list:
  52. yield ':'.join(proxy.xpath('./td/text()')[0:2])
  53. print 'finish kuaidaili fetching proxy ip'
  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).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. print 'finish 66ip fetching proxy ip'
  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. header = {
  78. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  79. 'Accept-Encoding': 'gzip, deflate, sdch',
  80. 'Accept-Language': 'zh-CN,zh;q=0.8',
  81. 'Cache-Control': 'max-age=0',
  82. 'Connection': 'keep-alive',
  83. # 'Cookie': 'Hm_lvt_f8bdd88d72441a9ad0f8c82db3113a84=1487640273; Hm_lpvt_f8bdd88d72441a9ad0f8c82db3113a84=1487640642',
  84. 'Host': 'www.youdaili.net',
  85. # 'If-Modified-Since': 'Tue, 21 Feb 2017 00:40:52 GMT',
  86. 'If-None-Match': "5c67-548ffa1d8ca68-gzip",
  87. 'Upgrade-Insecure-Requests': '1',
  88. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
  89. }
  90. tree = getHtmlTree(url, header=header)
  91. page_url_list = tree.xpath('.//div[@class="chunlist"]/ul/li/p/a/@href')[0:days]
  92. for page_url in page_url_list:
  93. html = requests.get(page_url, headers=header).content
  94. # print html
  95. proxy_list = re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}', html)
  96. for proxy in proxy_list:
  97. yield proxy
  98. print 'finish youdaili fetching proxy ip'
  99. @staticmethod
  100. @robustCrawl
  101. def freeProxyFourth():
  102. """
  103. 抓取西刺代理 http://api.xicidaili.com/free2016.txt
  104. :return:
  105. """
  106. url = "http://api.xicidaili.com/free2016.txt"
  107. html = requests.get(url).content
  108. for proxy in re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}', html):
  109. yield proxy
  110. print 'finish xici fetching proxy ip'
  111. @staticmethod
  112. @robustCrawl
  113. def freeProxyFifth():
  114. """
  115. 抓取guobanjia http://www.goubanjia.com/free/gngn/index.shtml
  116. :return:
  117. """
  118. url = "http://www.goubanjia.com/free/gngn/index.shtml"
  119. header = {
  120. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  121. 'Accept-Encoding': 'gzip, deflate, sdch',
  122. 'Accept-Language': 'zh-CN,zh;q=0.8',
  123. 'Cache-Control': 'max-age=0',
  124. 'Connection': 'keep-alive',
  125. # Cookie:auth=49b87d158a2b9e02589295bb9b74e8cf; JSESSIONID=BE57CA28BB0D8DDC119A542A75216B30; CNZZDATA1253707717=2116078297-1487635832-%7C1487641386; Hm_lvt_2e4ebee39b2c69a3920a396b87bbb8cc=1487641109; Hm_lpvt_2e4ebee39b2c69a3920a396b87bbb8cc=1487641408
  126. 'Host': 'www.goubanjia.com',
  127. 'Upgrade-Insecure-Requests': '1',
  128. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
  129. }
  130. tree = getHtmlTree(url, header=header)
  131. # 现在每天最多放15个(一页)
  132. for i in xrange(15):
  133. d = tree.xpath('.//table[@class="table"]/tbody/tr[{}]/td'.format(i + 1))[0]
  134. o = d.xpath('.//span/text() | .//div/text()')
  135. yield ''.join(o[:-1]) + ':' + o[-1]
  136. print 'finish guobanjia fetching proxy ip'
  137. if __name__ == '__main__':
  138. gg = GetFreeProxy()
  139. for e in gg.freeProxyFirst():
  140. print e
  141. for e in gg.freeProxySecond():
  142. print e
  143. for e in gg.freeProxyThird():
  144. print e
  145. # for e in gg.freeProxyFourth():
  146. # print e
  147. #
  148. # for e in gg.freeProxyFifth():
  149. # print e