Chapter 9. Image-Capturing Webbots

In this chapter, I’ll describe a webbot that identifies and downloads all of the images on a web page. This webbot also stores images in a directory structure similar to the directory structure on the target website. This project will show how a seemingly simple webbot can be made more complex by addressing these common problems:

In Chapter 17, you’ll expand on these concepts to develop a spider that downloads images from an entire website, not just one page.

Our image-capturing webbot downloads a target web page (in this case, the Viking Mission web page on the NASA website) and parses all references to images on the page. The webbot downloads each image, echoes the image’s name and size to the console, and stores the file on the local hard drive. Example 9-1 shows what the webbot’s output looks like when executed from a shell.

On this website, like many others, several unique images share the same filename but have different file paths. For example, the image /templates/logo.gif may represent a different graphic than /templates/affiliate/logo.gif. To solve this problem, the webbot re-creates a local copy of the directory structure that exists on the target web page. Figure 9-1 shows the directory structure the webbot created when it saved these images it downloaded from the NASA example.