最近實(shí)在是太流行采集了,本人是不喜歡采集的,但對采集的原理我卻很有興趣進(jìn)行研究,拿到了網(wǎng)上采集常用函數(shù),對其進(jìn)行了一番研究,并實(shí)戰(zhàn),結(jié)果成功,撇開效率問題,采集原理并不復(fù)雜,大家可以在搜索吧輸入“采集”查看其原理。下面是一個采集的例子:
復(fù)制代碼 代碼如下:
%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
% Response.CodePage=65001%>
% Response.Charset="UTF-8" %>
%Server.Scripttimeout=9999999
response.expires = 0
response.expiresabsolute = Now() - 1
response.addHeader "pragma","no-cache"
response.addHeader "cache-control","private"
Response.CacheControl = "no-cache"
%>
%
'聲明取得目標(biāo)信息的函數(shù),通過XML組件進(jìn)行實(shí)現(xiàn)。
Function GetURL(url)
Set Retrieval = server.createobject("MSXML2.XMLHTTP")
With Retrieval
.Open "GET", url, False
.Send
If .Status>200 then '判斷文檔是否已經(jīng)解析完,以做客戶端接受返回消息
exit function
End If
' 二進(jìn)制轉(zhuǎn)字符串
GetURL = sTb(.responsebody)
end with
'對取得信息進(jìn)行驗(yàn)證,如果信息長度小于100則說明截取失敗
End Function
' 二進(jìn)制轉(zhuǎn)字符串,否則會出現(xiàn)亂碼的!
Function sTb(vin)
Const adTypeText = 2
Dim BytesStream,StringReturn
Set BytesStream = Server.CreateObject("ADODB.Stream")
With BytesStream
.Type = adTypeText
.Open
.WriteText vin
.Position = 0
.Charset = "GB2312"
.Position = 2
StringReturn = .ReadText
.Close
End With
Set BytesStream = Nothing
sTb = StringReturn
End Function
Function Newstring(Wstr,Strng)
Newstring=Instr(Lcase(Wstr),Lcase(Strng))
If Newstring=0 Then Newstring=Len(Wstr)
End Function
'聲明截取的格式,從Start開始截取,到Over為結(jié)束
Function GetKey(HTML,Start,Over)
Start=Newstring(HTML,start)
Over=Newstring(HTML,Over)
GetKey=Mid(HTML,Start,Over-start)
End Function
Dim Softid,Url,Html,Title
'采集百度知道
For i = 1 to 100
Url="http://zhidao.baidu.com/question/10000"i".html"
Html = GetURL(Url)
Question = GetKey(Html,"cq>","/cq>")
Answer = GetKey(Html,"ca>","/ca>")
Response.Write(Question"br />")
Response.Write(Answer)
Response.Write("采集成功")
Next
'打開數(shù)據(jù)庫,準(zhǔn)備入庫
'dim connstr,conn,rs,sql
'connstr="DBQ="+server.mappath("db1.mdb")+";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
'set conn=server.createobject("ADODB.CONNECTION")
'conn.open connstr
'set rs=server.createobject("adodb.recordset")
'sql="select [列名] from [表名] where [列名]='"Title"'"
'rs.open sql,conn,3,3
'if rs.eof and rs.bof then
'rs("列名")=Title
'rs.update
'set rs=nothing
'end if
'set rs=nothing
%>