|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471188816
Publication Date: 06/01/97
Chapter 19 SiteMapper
Microsofts Internet transfer control allows programs to transfer data across the World Wide Web in several ways. Chapter 18 shows how to use this control to upload data from a Web page to a remote server computer.
The Internet transfer controls OpenURL method allows a program to copy information in the other directionfrom a remote Web page to a local application. This chapter shows how a program can use this capability to search a group of Web pages automatically. The SiteMapper program begins by visiting a Web page. It searches that page for references to other Web pages that are located at the same site. It then visits those pages, looking for new Web addresses that it has not yet visited. It continues visiting new pages until it has visited every HTML document at the site that it can reach from the original document.
The following section, Using SiteMapper, describes SiteMapper from the users point of view. It explains how to enter a URL, select the types of files the program should locate, and interpret the results.
The Key Techniques section that follows lists the most important programming methods demonstrated by SiteMapper. The rest of the chapter describes those methods in detail.
Using SiteMapper
To run SiteMapper the user enters a Web address or URL (Universal Resource Locator) in the Start URL text box. At a minimum, the URL must contain the protocol http and a site name as in http://www.mysite.com. Next the user selects the types of files SiteMapper should locate using the check boxes.
When the user clicks the Start button, SiteMapper loads the document at the specified start URL. It searches the document for file names that match the file types checked. SiteMapper is not very clever about how it searches for these file names. It simply searches for the appropriate file extensions and assumes that they are parts of file names. This means the program will report file names contained within an HTML comment and that it will treat other strings that contain the correct extensions as file names even if they make no sense. For example, if the program found the text Download .GIF or .JPG files, it would treat the strings .GIF and .JPG as file names.
After SiteMapper has identified the files referenced in the original document, it loads any that are HTML files located at the same Web site and searches them for more URLs. It continues searching the documents it finds until it has examined every file it can reach.
When it is finished, the program displays the results of its search. For each document visited, the program lists the file name matches it found in that document. Figure 19.1 shows SiteMapper after it has visited the address http://Beauty/Ch19/ Mapper1.HTM. You can find copies of the Web pages it visited in the Ch19\Html directory on the compact disk.
The documents that SiteMapper visited to produce Figure 19.1 are related in the simple tree-like way shown in Figure 19.2. Arrows indicate links between the files. For example, the file Mapper2.HTM contains links to Mapper1.HTM, Mapper2a.HTM, and Mapper2b.HTM. These links are listed by SiteMapper in Figure 19.1. The Ch19\Home directory on the compact disk contains a more complicated example.
Key Techniques
This programs use of the Internet transfer control is extremely simple. The parts of the application that use the data retrieved by the control are more complicated.
FIGURE 19.1 SiteMapper mapping http://Beauty/Ch19/Mapper1.HTM.
FIGURE 19.2 The relationships among the HTML documents that produced Figure 19.1.
The following list briefly describes the techniques used by SiteMapper. The rest of the chapter explains these techniques in detail.
- Retrieving Web Documents. The Internet transfer controls OpenURL method retrieves a document from the Web. This section explains how a program can use the OpenURL method.
- Parsing URLs. SiteMapper must parse each URL so it can determine if it is within the same site as the original document. This section shows how to parse URLs.
- Expanding URLs. Many Web documents contain partial URLs. This section tells how SiteMapper expands partial URLs into complete addresses.
- Mapping Web Sites. SiteMapper uses a depth-first search to examine a Web site. This section explains how SiteMapper avoids visiting a Web document more than once.
- Modifying SiteMapper. SiteMapper searches a Web site for certain kinds of files. This section suggests changes to SiteMapper to make it perform other, similar tasks.
Retrieving Web Documents
Retrieving a document from the Web is easy using the Internet Transfer Controls OpenURL method. This method takes as its only parameter the Web address of the document it should open.
If the address indicates a directory on an FTP server, OpenURL returns a listing of the directory. Otherwise, if the address leads to a file, OpenURL returns the contents of the file.
SiteMapper uses the following code to fetch Web documents. It uses the On Error Resume Next statement to trap errors that may occur when OpenURL is running. If there is an error, the program adds an error message to its output text. A program can fail to retrieve a document over a network in many ways, so it is important that every network operation be protected with error-trapping code.
On Error Resume Next
txt = Inet1.OpenURL(the_url)
If Err.Number <> 0 Then
Results = Results & Error & _
Str$(Err.Number) & reading & _
the_url & vbCrLf & & _
Err.Description & vbCrLf
Exit Sub
End If
On Error GoTo 0
SiteMapper opens only HTML documents. Because these are text files, OpenURL returns a string containing the contents of the files. This string is pure HTML text and is not interpreted in any way by the OpenURL method. A Web browser built using OpenURL would parse the raw HTML code and decide what to display.
SiteMappers needs are much simpler than those of a browser. When it retrieves a document, it searches for certain strings within the document. For instance, if the user checks the .GIF button, SiteMapper looks for the string .GIF. This process is explained in more detail in the section Mapping Web Sites.
Parsing URLs
SiteMapper must parse each URL it encounters for two reasons. First, it must decide if the new URL is within the site the program is visiting. Second, it must determine which parts of a possibly incomplete URL are present so it knows what pieces it must add to create a complete URL. The section Expanding URLs explains how SiteMapper fills in any missing pieces.
The following section briefly describes the pieces of a URL. The section after that explains how SiteMapper parses URLs to identify their pieces.
|