getFreeProxy.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. import sys
  17. sys.path.append('../')
  18. try:
  19. from importlib import reload # py3 实际不会实用,只是为了不显示语法错误
  20. except:
  21. import sys # py2
  22. reload(sys)
  23. sys.setdefaultencoding('utf-8')
  24. from Util.utilFunction import robustCrawl, getHtmlTree
  25. from Util.WebRequest import WebRequest
  26. # for debug to disable insecureWarning
  27. requests.packages.urllib3.disable_warnings()
  28. class GetFreeProxy(object):
  29. """
  30. proxy getter
  31. """
  32. def __init__(self):
  33. pass
  34. @staticmethod
  35. @robustCrawl # decoration print error if exception happen
  36. def freeProxyFirst(page=10):
  37. """
  38. 抓取无忧代理 http://www.data5u.com/
  39. :param page: 页数
  40. :return:
  41. """
  42. url_list = ['http://www.data5u.com/',
  43. 'http://www.data5u.com/free/',
  44. 'http://www.data5u.com/free/gngn/index.shtml',
  45. 'http://www.data5u.com/free/gnpt/index.shtml']
  46. for url in url_list:
  47. html_tree = getHtmlTree(url)
  48. ul_list = html_tree.xpath('//ul[@class="l2"]')
  49. for ul in ul_list:
  50. yield ':'.join(ul.xpath('.//li/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://www.66ip.cn/mo.php?sxb=&tqsl={}&port=&export=&ktip=&sxa=&submit=%CC%E1++%C8%A1&textarea=".format(
  60. proxy_number)
  61. request = WebRequest()
  62. html = request.get(url).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. 抓取ip181 http://www.ip181.com/
  70. :param days:
  71. :return:
  72. """
  73. url = 'http://www.ip181.com/'
  74. html_tree = getHtmlTree(url)
  75. tr_list = html_tree.xpath('//tr')[1:]
  76. for tr in tr_list:
  77. yield ':'.join(tr.xpath('./td/text()')[0:2])
  78. @staticmethod
  79. @robustCrawl
  80. def freeProxyFourth():
  81. """
  82. 抓取西刺代理 http://api.xicidaili.com/free2016.txt
  83. :return:
  84. """
  85. url_list = ['http://www.xicidaili.com/nn', # 高匿
  86. 'http://www.xicidaili.com/nt', # 透明
  87. ]
  88. for each_url in url_list:
  89. tree = getHtmlTree(each_url)
  90. proxy_list = tree.xpath('.//table[@id="ip_list"]//tr')
  91. for proxy in proxy_list:
  92. yield ':'.join(proxy.xpath('./td/text()')[0:2])
  93. @staticmethod
  94. @robustCrawl
  95. def freeProxyFifth():
  96. """
  97. 抓取guobanjia http://www.goubanjia.com/free/gngn/index.shtml
  98. :return:
  99. """
  100. url = "http://www.goubanjia.com/free/gngn/index{page}.shtml"
  101. for page in range(1, 10):
  102. page_url = url.format(page=page)
  103. tree = getHtmlTree(page_url)
  104. proxy_list = tree.xpath('//td[@class="ip"]')
  105. for each_proxy in proxy_list:
  106. yield ''.join(each_proxy.xpath('.//text()'))
  107. @staticmethod
  108. @robustCrawl
  109. def freeProxySixth():
  110. """
  111. 抓取讯代理免费proxy http://www.xdaili.cn/ipagent/freeip/getFreeIps?page=1&rows=10
  112. :return:
  113. """
  114. url = 'http://www.xdaili.cn/ipagent/freeip/getFreeIps?page=1&rows=10'
  115. request = WebRequest()
  116. try:
  117. res = request.get(url).json()
  118. for row in res['RESULT']['rows']:
  119. yield '{}:{}'.format(row['ip'], row['port'])
  120. except Exception as e:
  121. pass
  122. if __name__ == '__main__':
  123. gg = GetFreeProxy()
  124. # for e in gg.freeProxyFirst():
  125. # print e
  126. # for e in gg.freeProxySecond():
  127. # print e
  128. # for e in gg.freeProxyThird():
  129. # print e
  130. for e in gg.freeProxySixth():
  131. print(e)
  132. # for e in gg.freeProxyFifth():
  133. # print(e)