昨天在网上看到一个防采集软件,说采集只访问当前网页,不会访问网页的图片、JS等,今天突然想到,通过动态程序和Js访问分别记录访问者的IP,然后进行IP判断,由于采集过程不会访问JS,采集的时候只会查到用动态程序记录的IP,而不会有通过JS记录的IP,从而实现网页程序的防采集。防采集的原理非常简单,首先放一段动态语句,把访问者的IP加入到数据库的一个表里,然后在页面底部加入一个JS,JS直接访问动态页面,将访问者的IP加入到数据库的另外一个表里。再次访问的时候,从两个表里读IP数据,然后判断时间差,如果只在第一个表里找到,在第二个表里找不到,或者时间差超过10秒,则认为是采集。优点1.部署简单,只要是动态语言就能很容易的实现,无需借助服务器端程序2.杀伤力大,几乎能封杀所有的采集过程缺点1.第一个缺点还是杀伤力大,如果需要实际使用需要考虑一些特殊情况,以免误杀已经杀掉搜索爬虫2.只适用于动态网页,静态页面就没法用了流程写的比较乱,不过原理本身就不是很复杂,下面附上程序例子,懂ASP的应该很快就能看懂。1.建立数据库表1:Ip1,字段Ip1_Adderss(文本),Ip1_Time(日期/时间,默认值=Now())表2:Ip2,字段Ip2_Adderss(文本),Ip2_Time(日期/时间,默认值=Now())2.Index.asp(仅动态代码,全部代码请见测试程序中) - <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
- <%
- Dim Conn,Rs,Sqlstr,Ip,IpTime,IpTime2,NewUser
- NewUser=0
- Set Conn = Server.CreateObject("Adodb.Connection")
- Set Rs=Server.Createobject("Adodb.RecordSet")
- ConnStr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("Data.mdb")
- Conn.Open ConnStr
- Ip=Request.ServerVariables("REMOTE_ADDR")
- Sqlstr="Select * From [Ip1] Where Ip1_Address='"&Ip&"' Order By Ip1_Id Desc"
- Rs.Open Sqlstr,Conn,1,3
- If Rs.Eof Then
- NewUser=1
- Application.Lock()
- Rs.AddNew()
- Rs("Ip1_Address")=Ip
- Rs.Update()
- Application.UnLock()
- Else
- IpTime=Rs("Ip1_Time")
- Application.Lock()
- Rs.AddNew()
- Rs("Ip1_Address")=Ip
- Rs.Update()
- Application.UnLock()
- End If
- Rs.Close
- If NewUser=0 Then
- Sqlstr="Select * From [Ip2] Where Ip2_Address='"&Ip&"' Order By Ip2_Id Desc"
- Rs.Open Sqlstr,Conn,1,3
- If Rs.Eof Then
- Rs.Close
- Response.Write("请勿采集!")
- Response.End()
- Else
- IpTime2=Rs("Ip2_Time")
- If DateDiff("s",IpTime2,IpTime)>10 Then
- Rs.Close
- Response.Write("请勿采集!")
- Response.End()
- End If
- End If
- Rs.Close
- End If
- %>
复制代码
3.Js.asp- <%
- Dim Conn,Rs,Sqlstr,Ip
- Set Conn = Server.CreateObject("Adodb.Connection")
- Set Rs=Server.Createobject("Adodb.RecordSet")
- ConnStr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("Data.mdb")
- Conn.Open ConnStr
- Ip=Request.ServerVariables("REMOTE_ADDR")
- Sqlstr="Select * From [Ip2]"
- Rs.Open Sqlstr,Conn,1,3
- Application.Lock()
- Rs.AddNew()
- Rs("Ip2_Address")=Ip
- Rs.Update()
- Application.UnLock()
- Rs.Close
- %>
复制代码
4.Get.asp- <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
- <%
- Response.Write(Server.HTMLEncode(GetHttpPage("http://localhost/Index.asp","GB2312")))
- '==============================
- '函 数 名:GetHttpPage
- '作 用:获取页面源代码函数
- '参 数:网址HttpUrl
- '==============================
- Function GetHttpPage(HttpUrl,Code)
- If IsNull(HttpUrl)=True Or HttpUrl="" Then
- GetHttpPage="A站点维护中!"
- Exit Function
- End If
- On Error Resume Next
- Dim Http
- Set Http=server.createobject("MSX"&"ML2.XML"&"HTTP")
- Http.open "GET",HttpUrl,False
- Http.Send()
- If Http.Readystate<>4 then
- Set Http=Nothing
- GetHttpPage="B站点维护中!"
- Exit function
- End if
- GetHttpPage=BytesToBSTR(Http.responseBody,Code)
- Set Http=Nothing
- If Err.number<>0 then
- Err.Clear
- GetHttpPage="C站点维护中!"
- Exit function
- End If
- End Function
- '==============================
- '函 数 名:BytesToBstr
- '作 用:转换编码函数
- '参 数:字符串Body,编码Cset
- '==============================
- Function BytesToBstr(Body,Cset)
- Dim Objstream
- Set Objstream = Server.CreateObject("ado"&"d"&"b.st"&"re"&"am")
- Objstream.Type = 1
- Objstream.Mode =3
- Objstream.Open
- Objstream.Write body
- Objstream.Position = 0
- Objstream.Type = 2
- Objstream.Charset = Cset
- BytesToBstr = Objstream.ReadText
- Objstream.Close
- set Objstream = nothing
- End Function
- %>
复制代码
|