Principles and common functions of ASP thief collection program

Source: Internet
Author: User

Collection Program Advantages: No need to maintain the website, because the data in the collection program comes from other websites, it will be updated with the updates of the website; can save server resources, generally the collection program only a few files, all webpage content comes from other websites. Disadvantages:

Unstable. If an error occurs on the target website, the program will also go wrong. If the target website is upgraded and maintained, the collection program must be modified accordingly. The speed is because it is called remotely, it must be slower than reading data on a local server.

I. Examples
The following is a brief description of the Application of XMLHTTP in ASP. CopyCode The Code is as follows: <%
'Common Functions
1. Enter the URL of the target webpage. The returned value gethttppage is the HTML code of the target webpage.
Function gethttppage (URL)
Dim HTTP
Set HTTP = server. Createobject ("msxml2.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 err. Clear
End Function
'2. If you use XMLHTTP to call a webpage with Chinese characters, you can use the ADODB. Stream component to convert the webpage.
Function bytestobstr (Body)
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 = "gb2312" 'to convert the original default UTF-8 encoding to gb2312 encoding, otherwise the xml http component directly calls a Web page with Chinese characters will get garbled
Bytestobstr = objstream. readtext
Objstream. Close
Set objstream = nothing
End Function
'Next try to call the http://www.google's HTML content
Dim URL, html
Url = "http://www.google ";
Html = gethttppage (URL)
Response. Write html
%>

2. Several common functions
Instr Functions
Description returns the position of the first occurrence of a character string (string2) in another string (string1.
Syntax instr (string1, string2)
For example:
Dim searchstring, searchchar
Searchstring = "http://www.google" 'the string to be searched in.
Searchchar = "blue1000" 'search for "blue1000 ".
Mybk = instr (searchstring, searchchar) 'returns 8
'If no value is found, "0" is returned. For example:
Searchchar = "BK"
Mybk = instr (searchstring, searchchar) 'returns 0
Mid Function
Description: returns a specified number of characters from a string.
Syntax mid (string, start, over)
For example:
Dim mybk
Mybk = mid ("Our BK (www. google) Design ", 7, 12) 'intercept string" Our BK (www. google) Design "12 characters after 7th characters", the value of mybk becomes "www. google"
Replace Function
Dim searchstring, searchchar
Searchstring = "Our BK design is a website construction resource website.
Searchstring = Replace (searchstring, "BK design", "www. Google") 'the value of searchstring becomes "our www. Google is a website construction resource website"

3. Intercept the HTML code of the specified area
For example, I only want to obtain the text section between "<TD>" and "</TD>" in the following HTML code:
<HTML>
<Title> BK (www. Google) Google search engine </title>
<Body>
<Table>
<Tr> <TD> </tr>
<Tr> <TD id = "content"> BK (www. Google) Google search engine is a site with many resources ...... </TD> </tr>
</Table>
</Body>
</Html>
<%
......
Dim strbk, start, over, rsbk
Strbk = gethttppage (webpage address)
Start = instr (strbk, "<TD id =" "content"> ") 'is used to locate the start position of a string. Someone may ask: the original code is <TD id = "content">. Why do you call <TD id = "" content ""> here? Answer: ASP (In VBScript, two double quotation marks are used to represent a double quotation mark, because double quotation marks are sensitive characters for the program .) Over = instr (strbk ,"... </TD> </tr> ") 'is used to locate the end of a string. 'You have to ask again: (why did the program call HTML code three more points before "... "Ah? A: tip: The above line also has a </TD> </tr>. If you use </TD> </tr> to locate the problem, the program mistakenly regards </TD> </tr> of the preceding line as the end part of the string to be obtained. Rsbk = mid (strbk, start, over-Start) 'is used to retrieve the string between the start character and the over character in strbk. As mentioned in the previous section of the MID function, over-start is used to calculate the distance between the start position and the end position, that is, the number of characters.
Response. Write (rsbk) 'the content obtained by the final output program
%>
Don't be too happy. When you run it, you will find that the HTML code on the page is incorrect. Why? Because the HTML code you get is: <TD id = "content"> BK (www. Google) Google search engine is a site with many resources...
See it? Incomplete HTML code! What should we do? Start = instr (strbk, "<TD id =" "content"> ") The statement obtains" <TD id = "content"> "in

The number of positions in strbk. Now we can add 17 after the program statement, then the program points the position to the character after <TD id = "content">.
Okay, the program will be changed to this:
<%
......
Dim strbk, start, over, rsbk
Strbk = gethttppage (webpage address)
Start = instr (strbk, "<TD id =" "content"> ") + 17
Over = instr (strbk ,"... </TD> </tr> ") 'here, you can remove the three dots by subtracting seven (-7 ).
Rsbk = mid (strbk, start, over-start)
Response. Write (rsbk)
%>
In this way, we can steal what we want and display it on our own page ~

4. delete or modify the obtained characters
Replace "BK (www. Google)" In rsbk with "BK ":
Rsbk = Replace (rsbk, "BK (www. Google)", "BK ")
You can also directly Delete "(www. Google:
Rsbk = Replace (rsbk, "(www. Google )","")
Now rsbk becomes: "BK Google search engine is a site with many resources ...... "Now. but in fact, the replace function may be unsuitable in some cases. For example, we want to remove all connections in a string. there may be many types of connections. Replace can only replace one of them. We cannot replace it with one corresponding replace function?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.