Web::Scraper hacks #2: Extract javascript and css content

miyagawa on 2007-09-09T16:52:07

This is inspired by an email from Renée Bäcker asking how to get content inside javascript tag. Because Web::Scraper's 'TEXT' mapping calls as_text method of HTML::Element, it doesn't get the content inside script and style tag.

Here's the code that works. It's kinda clumsy, and it'd be nice if there's much cleaner way to do this:

#!/usr/bin/perl
# extract Javascript code into 'code'
use strict;
use Web::Scraper;

my $s = scraper { process "script", code => sub { join '', $_->content_list; }; };