Do you know how to implement asp collection code? Today, the editor will introduce to you the tutorial on the implementation of Asp code collection. Interested friends follow the editor’s pace to see the specific content.
Collection begins
The first step is to analyze the page to be collected.
Use a browser to open the page you want to collect (such as: http://sports.sina.com.cn/k/2008-09-15/04593948756.shtml, you can use other pages). After opening, right-click to check the source file.
The second step is to find the location of the content to be collected.
If I want to collect the title and content on this page:
The title is between <h1 id="artibodyTitle" style="color:#03005C;"> and </h1>
The content is between the content begin --> and the content end --> of the content of the text
Pay attention to the uniqueness of the location. After finding it, you can use the search in the edit to see if it is unique and as unique as possible. If not, as first as possible. If it does not work, you can only replace it.
Step 3: Write the code
The code copy is as follows:< %
'Function: Asp collection code
'Author: wangsdong
'Note: Support original programs, please keep this information, thank you
url="http://sports.sina.com.cn/k/2008-09-15/04593948756.shtml"
str=getHTTPPage(url)
title=strcut(str,"<h1 id=""artibodyTitle"" style=""color:#03005C;"">","</h1>",2)
content=strcut(str,"<!-- text content begin -->","<!-- text content end -->",2)
response.write "News Title<br><b>"&title&"</b><br><br><br><br>News Content:<br>"&content
Function getHTTPPage(url)
On Error Resume Next
dim http
set http=Server.createobject("Microsoft.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
set http=nothing
If Err.number<>0 then
Response.Write "<p align='center'><font color='red'><b>The server errored in obtaining file content</b></font></p>"
Err.Clear
End If
End Function
Function BytesToBstr(body,Cset)
dim objstream
set objstream = Server.CreateObject("adodb.stream")
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
'Intercepting string, 1. Includes start and end characters, 2. Not including
Function strCut(strContent,StartStr,EndStr,CutType)
Dim strHtml, S1, S2
strHtml = strContent
On Error Resume Next
Select Case CutType
Case 1
S1 = InStr(strHtml,StartStr)
S2 = InStr(S1, strHtml,EndStr)+Len(EndStr)
Case 2
S1 = InStr(strHtml,StartStr)+Len(StartStr)
S2 = InStr(S1,strHtml,EndStr)
End Select
If Err Then
strCute = "<p align='center'>Nothing found what is needed.</p>"
Err.Clear
Exit Function
Else
strCut = Mid(strHtml,S1,S2-S1)
End If
End Function
% >
The above is the tutorial on the implementation of Asp collection code. Now that the editor outputs the contents you get, you can write these contents into the database, and the data is your own.