|
|
O'Reilly Book Excerpts: Spidering Hacks Spidering Hacksby Kevin Hemenway and Tara CalishainEditor's note: This week we offer two hacks from Spidering Hacks that will save you time and extra trips to your favorite web sites. And check back to this space next week for two more hacks from the book; the first will be on scraping all the URLs in a specified subcategory of the Yahoo directory; the second will be on using a bit of Perl to quickly find the word you're looking for in either an online dictionary or thesaurus. Hack #24: Painless RSS with Template::ExtractWouldn't it be nice if you could simply visualize what data on a page looks like, explain it in template form to Perl, and not bother with the need for parsers, regular expressions, and other programmatic logic? That's exactly what Template::Extract helps you do. One thing that I'd always wanted to do, but never got around to doing, was produce RSS files for all those news sites I read regularly that don't have their own RSS feeds. Maybe I'd read them more regularly if they notified me when something was new, instead of requiring me to remember to check.
One day, I was fiddling about with the Template Toolkit (http://www.template-toolkit.com/) and it dawned on me that all these sites were, at some level, generated with some templating engine. The Template Toolkit takes a template and some data and produces HTML output. For instance, if I have the following Perl data structure:
I can apply a template like so:
I'll end up with some HTML that looks like this:
Okay, you might think, very interesting, but how does this relate to scraping web pages for RSS? Well, we know what the HTML looks like, and we can make a reasonable guess at what the template ought to look like, but we want only the data. If only I could apply the Template Toolkit backward somehow. Taking HTML output and a template that could conceivably generate the output, I could retrieve the original data structure and, from then on, generating RSS from the data structure would be a piece of cake. Like most brilliant ideas, this is hardly original, and an equally brilliant man named Autrijus Tang not only had the idea a long time before me, but—and this is the hard part—actually worked out how to implement it. His Template::Extract Perl module (http://search.cpan.org/author/AUTRIJUS/Template-Extract/) does precisely this: extract a data structure from its template and output. I put it to work immediately to turn the blog of one of my favorite singers, Martyn Joseph (http://www.piperecords.co.uk/news/diary.asp), into an RSS feed. I'll use his blog for the example in this hack. First, write a simple bit of Perl to grab the page, and tidy it up to avoid tripping over whitespace issues:
This removes blank lines, DOS line feeds, and leading spaces. Once you've done this, take a look at the structure of the page. You'll find that blog posts start with this line:
and end with this one:
The interesting bit of the diary starts after the close of an HTML comment:
After a bit more observation, you can glean a template like this:
The special
You end up with a data structure that looks like this:
The XML::RSS Perl module [Hack #94] can painlessly turn this data structure into a well-formed RSS feed:
Job done—well, nearly. You see, it's a shame to have solved such a generic problem—scraping a web page into an RSS feed—in such a specific way. Instead, what I really use is the following CGI driver, which allows me to specify all the details of the site and the RSS in a separate file:
Now I can have a bunch of files that describe how to scrape sites:
When I point my RSS aggregator at the CGI script (http://blog.simon-cozens.org/rssify.cgi/martynj), I have an instant scraper for all those wonderful web sites that haven't made it into the RSS age yet. Template::Extract is a brilliant new way of doing data-directed screen scraping for structured documents, and it's especially brilliant for anyone who already uses Template to turn templates and data into HTML. Also look out for Autrijus's latest crazy idea, Template::Generate (http://search.cpan.org/author/AUTRIJUS/Template-Generate/), which provides the third side of the Template triangle, turning data and output into a template. Simon Cozens
|
|
|
|
|
||||||||