Top > Perl_de_RSS
AND OR

PerlでRSS

Perlを使って、静的なHTMLファイルから、RSSデータを生成したい。 某授業のため。

HTMLファイルの一覧をスキャン

あるディレクトリを再帰的に見る、のを書く予定。ファイルシステムからにするか、区ローラーっぽくするか…

HTML->RSSの生成

とりあえずこんな感じ。 Template::Extactも試してみたけど、フォーマットがきっちりしてないと、 うまくHTMLファイルから情報を拾えないみたい。 結局、HTML::TokeParserで無難に処理。

#!/usr/bin/perl -w

use strict;

use LWP::Simple;
use HTML::TokeParser;
use XML::RSS;

use Encode;
use Encode::Guess;
use encoding "euc-jp";
binmode (STDERR, ':raw :encoding(euc-jp)');


sub rss_format_times {
    my @now = gmtime();
    $now[4]++;
    $now[5] += 1900;
    my $ret = sprintf( "%04d-%02d-%02dT%02d:%02d:%02d+00:00",
                    $now[5], $now[4], $now[3], $now[2], $now[1], $now[0] );
    return $ret;
}

{
    # Step1. get HTML contents.
    #####################################################################
    my $url = shift;
    my $content = get ($url) or die "Can't get $url";
    # find encoding and decode to internal code.
    my $enc = guess_encoding($content, qw/euc-jp shiftjis 7bit-jis utf8/);
    $content = decode($enc->name, $content);

    # Step2. get RSS channel information.
    #####################################################################
    my $stream = HTML::TokeParser->new(\$content);

    # Step3. initalize RSS and set RSS channel.
    #####################################################################
    my $rss = XML::RSS->new({version => "0.91"});

    my $tag = $stream->get_tag("title");
    my $page_title = $stream->get_trimmed_text("/title");

    $rss->channel (
                   title => $page_title,
                   link  => $url,
                   description => $page_title . ": Web Design in Hyogo Universit
y.",
                   );

    # Step4. get RSS item info from HTML contents.
    #####################################################################
    my $tag2;
    my $title;
    my $aname;
    my $description;
    # loop for each <h2> group.
    while($tag = $stream->get_tag("h2")){
        $tag2  = $stream->get_tag("a");
        # Is there aname attribute ?
        if ($tag2->[1]{name}) {
            # get out <a>...</a> and  "aname" attribute.
            $aname = $tag2->[1]{name};
            # get out <h2>...</h2>.
            $title = $stream->get_trimmed_text("/h2");
            # get out <p>...</p>.
            $tag2  = $stream->get_tag("p");
            $description = $stream->get_trimmed_text("/p");

            $rss->add_item (
                            title => $title,
                            link => $url . "#". $aname,
                            description => $description,
                            );
        }
    }

    # Step5. output RSS feed.
    #####################################################################
    print $rss->as_string;
    $rss->save("test.rdf");
}

情報収集

Perl入門

RSS関係

  • XML::ParserとXML::RSSはCPANからインストールできず -- 2006-08-22 (火) 15:20:45
  • 結局、Vineのapt-getからインストール…(apt-get install perl-XML-RSS) -- 2006-08-22 (火) 15:21:19
  • XXebkUlcX -- idcsjumziqw 2011-04-03 (日) 21:42:48

タグ抽出

日本語処理

CPAN

}}


リロード   差分   ホーム 一覧 検索 最終更新 バックアップ リンク元   ヘルプ   最終更新のRSS
Last-modified: Tue, 11 Mar 2014 20:20:22 JST (3691d)