ASP采集基础教程(一)

网络整理 - 09-04
ASP采集其它就是利用微软自带的XMLHTTP组件取得远程页面的源代码,再经过程序的过滤替换处理转换成我们想要的数据。我们也不管用什么,反正我教你的就是用一段代码取得目标代码,再把目标代码加工一下。然后直接显示啊还是写入数据库。就这么简单!

  引用组件CreateObject("Microsoft.XMLHTTP")--->取得目标代码(get)--->数据处理(截取,过滤,替换)--->直接显示(小偷)或 写入数据库(采集)。

  说了这么多是要开始实战了. 先写个刚刚讲到的引用组件和取得代码的函数 

<%'获取远程HTML 
Function GetURL(url) 
 Set Retrieval = CreateObject("Microsoft.XMLHTTP") 
 With Retrieval 
  .Open "GET", url, False
  .Send 
  GetURL = .responsebody 
 if len(.responsebody)<100 then
  response.write "获取远程文件 <a href="&url&" target=_blank>"&url&"</a> 失败。"
  response.end
 end if
 End With 
 Set Retrieval = Nothing 
End Function%>


  
  上面这段函数的URL值就是我们的目标网页,也就是要偷的页面了。
  当然我们得到的代码是以二进制返回给我们的,那我们得把他转换成我们要的字符,怎么办?
  怕什么?我自己不会写难道还不会上网找啊。找到了,怎么实现的我们当然不管它了,只要能用就行了,函数代码如下:

<%'二进制转字符串
function bytes2bstr(vin) 
 strreturn = "" 
 for i = 1 to lenb(vin) 
 thischarcode = ascb(midb(vin,i,1)) 
 if thischarcode < &h80 then 
  strreturn = strreturn & chr(thischarcode) 
 else 
  nextcharcode = ascb(midb(vin,i+1,1)) 
  strreturn = strreturn & chr(clng(thischarcode) * &h100 + cint(nextcharcode)) 
  i = i + 1 
 end if 
 next 
 bytes2bstr = strreturn 
end function%>



  有了这两个函数基本上可以实现简单的小偷了。我今天就找天空的软件资讯开刀,地址是:

  1、建立一个ASP文件,我建的是index.asp,定义一下地址和调用函数

<%dim url,html
    url=""
    Html=GetURL(url) '把地址url传递到GetURL这个函数
       Html=Bytes2BStr(Html) '二进制转换成字符一下%>



  再把刚刚两个函数写进去,最后看一下是否偷到了,我们“Response.write html”一下。

  整个文件内容就是:

<%On Error Resume Next
Server.ScriptTimeOut=9999999
dim url,html
    url=""
    Html=GetURL(url) '把地址url传递到GetURL这个函数
       Html=Bytes2BStr(Html) '二进制转换成字符一下
' 获取远程HTML
Function GetURL(url) 
    Set Retrieval = CreateObject("Microsoft.XMLHTTP") 
    With Retrieval 
        .Open "GET", url, False
        .Send 
        GetURL = .responsebody 
       if len(.responsebody)<100 then
              response.write "获取远程文件 <a href="&url&" target=_blank>"&url&"</a> 失败。"
              response.end
       end if
    End With 
    Set Retrieval = Nothing 
End Function
' 二进制转字符串
function bytes2bstr(vin) 
       strreturn = "" 
       for i = 1 to lenb(vin) 
       thischarcode = ascb(midb(vin,i,1)) 
       if thischarcode < &h80 then 
              strreturn = strreturn & chr(thischarcode) 
       else 
              nextcharcode = ascb(midb(vin,i+1,1)) 
              strreturn = strreturn & chr(clng(thischarcode) * &h100 + cint(nextcharcode)) 
              i = i + 1 
       end if 
       next 
       bytes2bstr = strreturn 
end function
Response.write html%>



  试了一下,果真取到了。有图为证:



  为什么我们得到的内容图片啊链接啊什么的都不会对呢?那是因为我们只是仅仅得到目标的源码代并在我们的文件上显示而已。怎么才能显示正确的图片和正确的链接呢,那就是我们下一篇要讲的数据处理部分了。如何?学到了吗?换其它地址试试!