The "thief" mentioned here refers to the powerful feature provided by XMLHTTP components in XML in ASP, captures data on the remote website (picture, web pages, and other files) to the local, and displayed after various processes. The page or a class of programs entering into the database. You can pass this thief program, complete the task that seems to be completely impossible, so that after stealing a station, turn into your own page, or save some data (articles, pictures) to some stations to Used in the local database. The advantages of "thief" are: no need to maintain the website, because the data in the thief program is from other websites, which will update with the website update; save a large number of server resources, the general thief process, several files, all web content They are all from other websites. The disadvantage is: unstable, if the target site is wrong, the program will also make mistakes, and if the target website is upgraded, then the thief program should be modified accordingly; speed, because it is remote call, speed, and read data on the local server Compared to, it must be slower. How, it sounds amazing? Let's start to learn some "thief" procedures!
Let's take a simple point to study, the weather forecasting procedure on the QQ website
code show as below:
<% On error resume next server.scripttimeout = 9999999 function gethttppage (path) T = getBody (path) gethttppage = bytestobstr (t, "gb2312") end function
'First, some initialization settings of the thief program, the role of the above code is ignored all non-poorly wrong errors, respectively, set the running timeout time of the thief program, which does not have an error that runs timeout), converted the original default UTF-8 encoding is converted into GB2312 encoding, otherwise it will be garbled with the web page with the XMLHTTP component.
Function GetBody (url) on error resume next Set Retrieval = CreateObject ( "Microsoft.XMLHTTP") With Retrieval .Open "Get", url, False, "", "" .Send GetBody = .ResponseBody End With Set Retrieval = Nothing End FUNCTION
'Call the XMLHTTP component to create an object and perform initialization settings.
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 FunctionFunction Newstring (wstr, strng) Newstring = Instr (lcase (wstr), lcase (strng)) if Newstring <= 0 then Newstring = Len (wstr) END FUNCTION
'Handling the data that can be caught needs to call the AdoDB.Stream component and perform initialization settings. %>
'The following is the page display section
<% DIM WSTR, STR, URL, START, OVER, and CITY 'Define some variables that need to be used
City = Request.QueryString ("ID") 'The ID variable (ie, the user selected by the user) is assigned to the id
URL = "http://appnews.qq.com/cgi-bin/news_qq_search?city=" & city "" This settings you need to capture the page address, of course you can also specify an address without using variables
WSTR = gethttppage (URL) 'Get all the data for the specified page
Start = newstring (WSTR, "") 'This sets the heads of the data that need to be processed. This variable should be set to different situations, and the specific content can be determined by viewing the source code of the page you need to capture. Because we need to grab the entire page in this program, set it to all capture. Note that the content set must be unique, and cannot be repeated.
Over = newstring (WSTR, " HTML>") and Start are the tail of the data that needs to be processed, the same, the content of the settings must be unique in the page.
Body = MID (WSTR, START, OVER-START) Set the range of display pages
'When you use Qiankun Move , you can replace the characters specified in the data by replacing.
Body = Replace (Body, "Skin1", "Weather Forecast - Sk Network") Body = Replace (Body, "http://appnews.qq.com/cgi-bin/news_qq_search?city", "tianqi.asp? id ")
The replacement work has been completed in this program. If you have any other needs, you can continue similar alternative operations. Response.write body%>
After replacing the content you need to modify, you can display the modified content on the page. To the end of this program
Procedure Usage and Result: After removing the above code, saved to tianqi.asp after removing the above code, and uploaded to the space that supports ASP and XML, running in the browser. You can further interface beauty or program optimization on this program. The above is just some primary applications for XMLHTTP components. It is actually a lot of features that it can implement, such as saving remote pictures to local servers, with AdoDb.stream components, can save the acquired data into the database. The role and use range of thieves are wide. But you can't take a violation of law!
Maybe there may be someone to ask, is this "thief" program just ASP's patents? Non-also, PHP can achieve the same effect like the FOPEN function. Due to the various characteristics of PHP itself, the written thieves and ASPs have obvious advantages in volume and implementation efficiency, but limited to space, here It will not be explained.