Example analysis of asp collection principle collection start
The first step is to analyze the pages you want to capture.
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 also use other pages). After opening, right-click to view 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 location of this page:
The title is between <h1 id=artibodyTitle style=color:#03005C;> and </h1>
The content is between <!-- Text content begin --> and <!-- Text content end -->
Pay attention to the uniqueness of the location. After you find it, you can use the search in editing to see if it is unique. Try to be unique. If not, try to be the first one. If it still doesn't work, you can only replace it.
The third step is to write code
Copy the code code as follows:
<%
'Function: asp collection code
'Author: wangsdong
'Remarks: Support original programs, please keep this information, thank you
url=http://sports.sina.com.cn/k/2008-09-15/04593948756.shtml
str=getHTTPage(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>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 has an error in obtaining the 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
'Intercept the string, 1. including the starting and ending characters, 2. excluding
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'>The required content was not found. </p>
Err.Clear
Exit Function
Else
strCut = Mid(strHtml,S1,S2-S1)
End If
End Function
%>
That's it. I will output the obtained content now, and you can write the content into the database, so that the data is yours.