PerlのWeb::Scraperでスクレイピング&RSS化

#!/usr/bin/perl
 
use strict;
use warnings;
use Web::Scraper;
use URI;
# use YAML;

use XML::TreePP;
use XML::FeedPP;

my $uri = 'https://www.police.pref.ehime.jp/sokuho/sokuho.htm';
 
my $scraper = scraper {
    process '#main2 > tbody > tr:nth-child(3) > td > dl > dt', 'title[]' => 'TEXT';
    process '#main2 > tbody > tr:nth-child(3) > td > dl > dt + dd', 'content[]' => 'TEXT';
};

# warn Dump $scraper->scrape((URI->new($uri));

my $res = $scraper->scrape(URI->new($uri));

# Wide character対策
# binmode(STDOUT, ":utf8");

my $feed = XML::FeedPP::RSS->new();
my $now = time();

$feed->title( "愛媛の事件・事故速報" );
$feed->description( "愛媛の事件・事故速報" );
$feed->link( "https://www.police.pref.ehime.jp/sokuho/sokuho.htm" );
$feed->pubDate( $now );

for (my $i = 0; $i < $#{$res->{title}}; $i++) {

    # print $res->{"title"}[$i],"\n";
    # print $res->{"content"}[$i],"\n";
    
    $feed->add_item(link => $uri, title => $res->{"title"}[$i], description => $res->{"content"}[$i]);

}

#ファイル保存
$feed->to_file( "ehime-jiken.rss" );

print "Content-Type: text/xml; charset=UTF-8\n\n";
print $feed->to_string( "UTF-8" );

kujirahand.com

上記Perlのプログラムをtest.cgiに保存して
Google Apps Scriptを使ってCron代わりにしてアクセスするとehime-jiken.rssが作成されます。

function getURL() {
var apiURL = "http://example.com/test.cgi";
var r = UrlFetchApp.fetch(apiURL);
if (r.getResponseCode() == 200) {
var body = r.getContentText();
Logger.log(body);
}
}
imabari.hateblo.jp
imabari.hateblo.jp
imabari.hateblo.jp