前言

爬虫和反爬虫日益成为每家公司的标配系统。

爬虫在情报获取、虚假流量、动态定价、恶意攻击、薅羊毛等方面都能起到很关键的作用,所以每家公司都或多或少的需要开发一些爬虫程序,业界在这方面的成熟的方案也非常多。

有矛就有盾,每家公司也相应的需要反爬虫系统来达到数据保护、系统稳定性保障、竞争优势保持的目的。

像安全与黑客从来都是相辅相成一样。

爬虫与反爬虫也是在双方程序员的斗智斗勇的过程不断发展和成长的。

简单的反爬虫:

1.通过Headers反爬虫

从用户请求的Headers反爬虫是最常见的反爬虫策略。很多网站都会对Headers的User-Agent进行检测,还有一部分网站会对Referer进行检测(一些资源网站的防盗链就是检测Referer)。如果遇到了这类反爬虫机制,可以直接在爬虫中添加Headers,将浏览器的User-Agent复制到爬虫的Headers中;或者将Referer值修改为目标网站域名。对于检测Headers的反爬虫,在爬虫中修改或者添加Headers就能很好的绕过。

对于第二种情况,可以在每次请求后随机间隔几秒再进行下一次请求。有些有逻辑漏洞的网站,可以通过请求几次,退出登录,重新登录,继续请求来绕过同一账号短时间内不能多次进行相同请求的限制。

参数加密:js 处理过程可以写的很复杂,以至于爬虫程序员没法分析。

今天就说一说基本的反爬虫,实现方式是拒绝频繁访问的ip,首先我们要实现 IHttpModule接口

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.SessionState;
  7. using System.Configuration;
  8. namespace MyHttp
  9. {
  10. public class UrlReWrite : IHttpModule
  11. {
  12. /// <summary>
  13. /// 单个IP最大连接限制数量
  14. /// </summary>
  15. private int rowCount = Convert.ToInt32(ConfigurationSettings.AppSettings["HttpRowCount"]);
  16. /// <summary>
  17. /// 指定区域时间范围 单位分
  18. /// </summary>
  19. private int httpTime = Convert.ToInt32(ConfigurationSettings.AppSettings["HttpTime"]);
  20. public void Init(HttpApplication application)
  21. {
  22. application.BeginRequest += (new
  23. EventHandler(this.Application_BeginRequest));
  24. application.EndRequest += (new
  25. EventHandler(this.Application_EndRequest));
  26. }
  27. private void Application_BeginRequest(Object source, EventArgs e)
  28. {
  29. HttpApplication Application = (HttpApplication)source;
  30. HttpContext ctx = Application.Context;
  31. //IP地址
  32. string isIp = ctx.Request.UserHostAddress;
  33. if (ctx.Application["time"] == null)
  34. {
  35. ctx.Application["time"] = DateTime.Now;
  36. }
  37. else
  38. {
  39. DateTime isTime = (DateTime)ctx.Application["time"];
  40. int timeTract = Convert.ToInt32(DateTime.Now.Subtract(isTime).Minutes.ToString());
  41. if (timeTract > (httpTime - 1))
  42. {
  43. ctx.Application["time"] = null;
  44. ctx.Application["myip"] = null;
  45. }
  46. }
  47. if (ctx.Application["myip"] != null && ctx.Application["myip"] is CartIp)
  48. {
  49. CartIp cartIp = (CartIp)ctx.Application["myip"];
  50. cartIp.Insert(isIp);
  51. ctx.Application["myip"] = cartIp;
  52. if (cartIp.GetCount(isIp) > rowCount)
  53. {
  54. ctx.Response.Clear();
  55. ctx.Response.Close();
  56. }
  57. }
  58. else
  59. {
  60. CartIp cartIp = new CartIp();
  61. cartIp.Insert(isIp);
  62. HttpContext.Current.Application["myip"] = cartIp;
  63. }
  64. }
  65. private void Application_EndRequest(Object source, EventArgs e)
  66. {
  67. }
  68. public void Dispose()
  69. {
  70. }
  71. }
  72. }

 

ListIp 类

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace MyHttp
  5. {
  6. [Serializable]
  7. public class ListIp
  8. {
  9. private string ip;
  10. private int count;
  11. /// <summary>
  12. /// IP地址
  13. /// </summary>
  14. public string IP
  15. {
  16. get { return ip; }
  17. set { ip = value; }
  18. }
  19. /// <summary>
  20. /// 累加数量
  21. /// </summary>
  22. public int Count
  23. {
  24. get { return count; }
  25. set { count = value; }
  26. }
  27. }
  28. [Serializable]
  29. public class CartIp
  30. {
  31. public CartIp()
  32. {
  33. if (_listIp == null)
  34. {
  35. _listIp = new List<ListIp>();
  36. }
  37. }
  38. private List<ListIp> _listIp;
  39. public List<ListIp> _ListIp
  40. {
  41. get { return _listIp; }
  42. set { _listIp = value; }
  43. }
  44. /// <summary>
  45. /// 添加IP
  46. /// </summary>
  47. public void Insert(string ip)
  48. {
  49. int indexof = ItemLastInfo(ip);
  50. if (indexof == -1)
  51. {
  52. //不存在
  53. ListIp item = new ListIp();
  54. item.IP = ip;
  55. _listIp.Add(item);
  56. }
  57. else
  58. {
  59. _listIp[indexof].Count += 1;
  60. }
  61. }
  62. //判断IP是否存在
  63. public int ItemLastInfo(string ip)
  64. {
  65. int index = 0;
  66. foreach (ListIp item in _ListIp)
  67. {
  68. if (item.IP == ip)
  69. {
  70. return index;//存在
  71. }
  72. index += 1;
  73. }
  74. return -1;//不存在
  75. }
  76. /// <summary>
  77. /// 获得IP的数量
  78. /// </summary>
  79. /// <param name="ip"></param>
  80. /// <returns></returns>
  81. public int GetCount(string ip)
  82. {
  83. foreach (ListIp item in _ListIp)
  84. {
  85. if (item.IP == ip)
  86. {
  87. return item.Count;//存在
  88. }
  89. }
  90. return -1;//不存在
  91. }
  92. }
  93. }

在web.config 配置访问规则

  1. <appSettings>
  2. <add key="HttpRowCount" value="100"/>
  3. <add key="HttpTime" value="10"/>
  4. </appSettings>
  5. <system.web>
  6. <httpModules>
  7. <add name="UrlReWrite" type="MyHttp.UrlReWrite"/>
  8. </httpModules>
  9. </system.web>

 

版权声明:本文为ZaraNet原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/ZaraNet/p/9788297.html