Collection started
The first step is to analyze the page to be collected.
Open the page to be collected in a browser (for example, http://sports.sina.com.cn/k/2008-09-15/04593948756.shtml, you can access another page), right-click it, and check the source file.
Step 2: locate the content to be collected.
Assume that I want to collect the title and content location on this page:
The title is between <H1 id = "artibodytitle" style = "color: # 03005c;"> and
Content in <! -- Begin --> and <! -- Body content end -->
Pay attention to the uniqueness of the location. After finding the location, you can use the search in editing to see if it is unique. If not, try to be the first one. If not, change only
Step 3: Write Code copy Code the code is as follows: <%
'function: ASP collection code
' Author: wangsdong
'remarks: original Programs are supported. Keep this information, thank you
url = "http://sports.sina.com.cn/k/2008-09-15/04593948756.shtml"
STR = gethttppage (URL)
Title = strcut (STR, "
", " ", 2)
content = strcut (STR," "," ", 2)
response. write "news title
" & Title & "
news content:
"& 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> An error occurred while obtaining the file content on the server </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
'Truncation string, 1. Including 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'> the required content is not found. </P>"
Err. Clear
Exit Function
Else
Strcut = mid (strhtml, S1. S2-S1)
End if
End Function
%>
In this way, I will output the content now. You can write the content into the database, so that the data is yours.