asp 采集實(shí)戰(zhàn)代碼
更新時(shí)間:2007年08月01日 19:46:14 作者:
最近實(shí)在是太流行采集了,本人是不喜歡采集的,但對(duì)采集的原理我卻很有興趣進(jìn)行研究,拿到了網(wǎng)上采集常用函數(shù),對(duì)其進(jìn)行了一番研究,并實(shí)戰(zhàn),結(jié)果成功,撇開(kāi)效率問(wèn)題,采集原理并不復(fù)雜,大家可以在搜索吧輸入“采集”查看其原理。下面是一個(gè)采集的例子:
<%@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ù),通過(guò)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
'對(duì)取得信息進(jìn)行驗(yàn)證,如果信息長(zhǎng)度小于100則說(shuō)明截取失敗
End Function
' 二進(jìn)制轉(zhuǎn)字符串,否則會(huì)出現(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開(kāi)始截取,到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
'打開(kāi)數(shù)據(jù)庫(kù),準(zhǔn)備入庫(kù)
'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
%>
復(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ù),通過(guò)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
'對(duì)取得信息進(jìn)行驗(yàn)證,如果信息長(zhǎng)度小于100則說(shuō)明截取失敗
End Function
' 二進(jìn)制轉(zhuǎn)字符串,否則會(huì)出現(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開(kāi)始截取,到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
'打開(kāi)數(shù)據(jù)庫(kù),準(zhǔn)備入庫(kù)
'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
%>
相關(guān)文章
ASP下的簡(jiǎn)潔的多重查詢的方法與函數(shù) 真不錯(cuò)
ASP下的簡(jiǎn)潔的多重查詢的方法與函數(shù) 真不錯(cuò)...2007-10-10CreateKeyWord asp實(shí)現(xiàn)的由給定的字符串生成關(guān)鍵字的代碼
CreateKeyWord asp實(shí)現(xiàn)的由給定的字符串生成關(guān)鍵字的代碼...2007-09-09ASP中巧用Split()函數(shù)生成SQL查詢語(yǔ)句的實(shí)例
有時(shí)候我們需要根據(jù)參數(shù)來(lái)實(shí)現(xiàn)多種條件查詢,這里就是通過(guò)split函數(shù)將參數(shù)分割為多個(gè)2014-04-04Script.VBS.Agent.ai juan.vbs專(zhuān)殺
2008-01-01新手asp編程的基本法則與常見(jiàn)錯(cuò)誤注意事項(xiàng)
在論壇看到很多帖子代碼中都有一個(gè)共同的基本錯(cuò)誤,字段類(lèi)型錯(cuò)誤。程序和數(shù)據(jù)庫(kù)是緊緊相連的,數(shù)據(jù)庫(kù)字段文本型或時(shí)間型的都使用單引號(hào)2008-07-07asp中去除html中style,javascript,css代碼
剔除頁(yè)面中html中除文字以外的任何代碼,剛才發(fā)布了php版這個(gè)是ASP中的版本。2010-10-10GetPaing 函數(shù)之a(chǎn)sp采集函數(shù)中用到的獲取分頁(yè)的代碼
GetPaing 函數(shù)之a(chǎn)sp采集函數(shù)中用到的獲取分頁(yè)的代碼...2007-09-09