| Simple HTML page scraping with Delphi | |||||||||||||||||||||
|
|
|||||||||||||||||||||
| This article will show you the techniques needed to download an HTML page from the Internet, do some page scraping (using regular expressions for pattern matching) and present the information in more *situation-friendly* manner. | |||||||||||||||||||||
|
|
|||||||||||||||||||||
|
|
|||||||||||||||||||||
As you already know, About Delphi Programming
covers all aspects of Delphi/Kylix development, including articles, chat,
forum, RTL reference, glossary, free code VCL and much more.
NOTE: We've recently changed the way new postings are presented on this site. The "What's new and Hot" section has been transformed to "In the Spotlight" BLOG entries. Even though the ideas in this article are still "valid", if you want to grab the *Current Headlines* from the About Delphi Programming site, please visit the "About Delphi Programming *Current Headlines* sticker" page! There's one section of this site that brings you the latest news: compilation of hot, new and updated materials on the About Delphi Programming site. This page is located at "What's New and Hot". Every time you visit the site I strongly suggest you to open that page and see if some new items were added to the site. To enable Delphi programming related site web masters to add this
valuable information to their pages, I've developed the "About
Delphi Programming *NEW and HOT* sticker". It's a java script file, web
masters need to include in their web pages to give their viewers the
ultimate source of professional Delphi / Kylix programming content, updated
frequently. This java script file (.js) is created by a Delphi developed utility. This application downloads the What's New and Hot page from this site, then uses some regular expressions to extract the correct data from the page and finally creates a .js file using the extracted data. The idea of this article is to show you the techniques used to download a page from the Internet, do some page scraping and finally present the information in more "situation-friendly" manner. The key to data extraction methods described in this article is to convert the existing HTML document to more "situation-friendly" source. These are the steps we'll be discussing:
Note: The Sticker java script file described above uses the same techniques we are to discuss in this article - it is just enriched with HTML tags, due to the fact that JavaScript document.write method adds well formatted HTML code to an existing document. What's important is that the core of the Sticker .js is the data I'll show you how to extract. Preparing the Delphi Project Retrieval of HTML source documents Another approach, if you have Delphi 6, is to use the TDownloadURL object. The TDownloadURL object, defined in ExtActns.pas unit, is designed for saving the contents of a specified URL to a file. Here's the code that uses the TDownloadURL to download the "What's New and Hot" page from this site.
This function, Download_HTM, downloads a file from the URL specified in the sURL parameter, and saves this file locally under a sLocalFileName name. The function returns True if it succeeds, False otherwise. Of course, this function is to be called from the Button1 OnClick event handler. You can see the code below. Note that, locally, the file is saved as c:\temp_adp.newandhot.
Note: In the process of downloading a file, the TDownloadURL periodically generates an OnDownloadProgress event, so that you can provide users with feedback about the process. I'll leave this for you to implement. Now, that we have the HTM page locally on the disk we can use the techniques for handling ASCII files from Object Pascal code. Processing the HTML document
If you open up the downloaded file with the Notepad, you should notice that the information we want to extract is placed inside <!--DCnt--> and </!--DCnt--> tags. After you extract that part, you have to make sure any server or client side scripting is excluded - such text usually appears between the <script> and </script> tags. What remains is an HTML code, with 10 items formatted like:
Now, this "item" holds 4 pieces of interesting information. In the code, this item is a held in the ItemBuf string variable. Marked red is the URL of the particular news item. Marked blue is the title of this item. The description is green, and the date and location is maroon. To get the particular element of information, you can use the following code:
Finally you transform each item to 4 string type variables and display the information in ListView. I'm not going to bother you with the project details here, be sure to see the entire code, you'll have plenty to play with. This is the project at run-time:
If you have any questions or comments to this article, please post them on the Delphi Programming Forum. p.s. |
|||||||||||||||||||||