[ ホーム | 一覧 | 検索 | 最終更新 | ヘルプ ] [ 新規 ]

KAWANO's PukiWiki Plus! - Perl_de_RSS のバックアップ(No.9)

AND OR
  • バックアップ一覧
  • 差分 を表示
  • 現在との差分 を表示
  • 現在との差分 - Visual を表示
  • ソース を表示
  • Perl_de_RSS へ行く。
    • 1 (2006-08-19 (土) 06:58:15)
    • 2 (2006-08-19 (土) 09:46:04)
    • 3 (2006-08-19 (土) 15:56:53)
    • 4 (2006-08-20 (日) 09:49:23)
    • 5 (2006-08-22 (火) 07:08:27)
    • 6 (2006-08-22 (火) 07:48:49)
    • 7 (2007-02-24 (土) 07:05:44)
    • 8 (2007-02-24 (土) 07:10:26)
    • 9 (2011-04-03 (日) 12:42:48)

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入門

  • http://jp.rubyist.net/PerlMa/wiki.cgi?page=20060401-FirstStepPerl
    • メタ情報
  • http://www.kiwi-us.com/~mizusawa/penguin/html_hint/perlscriptbox.html
    • Tips集
▲ ▼

RSS関係

  • http://diary.noasobi.net/etc/yayakosi.html
  • http://www.kanzaki.com/docs/sw/rss-generation.html
  • http://www.rfs.jp/sb/perl/10/rss01.html
  • http://www.hyuki.com/techinfo/index.html
  • http://blog.bulknews.net/cookbook/blosxom/rss/genfeed.html
  • http://perltips.twinkle.cc/perl/xml_perl/
    • PerlやPHPやAjaxの小技集から「XML(Perl)」の記事集
  • http://www.hyuki.com/yukiwiki/wiki.cgi?BlogFeed
    • すでにある(?)ツールのドキュメントの和訳
  • http://www.nishishi.com/soft/rssmaker/
    • RSSを生成するソフト(自動抽出可能)
  • http://naoya.dyndns.org/~naoya/mt/archives/000833.html
    • Template::ExtactとXML::RSSを使う方法(テンプレートエンジンを使うのがミソ)
  • http://www.affrc.go.jp/ja/rss/csv2rss.html
  • http://www.nishishi.com/blog/2006/01/perl_xmlrss.html
  • http://www.nurs.or.jp/~sug/homep/rss/rss2.htm
    • 日付の作成部分を参考に
  • http://allabout.co.jp/career/cgiperl/closeup/CU20050131A/
    • RSSの解説と、RSS->HTMLの話
  • 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
▲ ▼

タグ抽出

  • http://www.kawa.net/works/perl/html/tagparser.html
    • モジュール
  • http://blog.bulknews.net/cookbook/blosxom/rss/rss_auto_discovery.html
    • RSSの場合
  • http://q.hatena.ne.jp/1119119220
    • 人力検索はてなから(1)
  • http://q.hatena.ne.jp/1119626640
    • 人力検索はてなから(2)
  • http://q.hatena.ne.jp/1127249236
    • 人力検索はてなで「HTML::Parser」
  • http://digit.que.ne.jp/work/wiki.cgi?Perl%E3%83%A1%E3%83%A2%2FTips
    • まとめっぽい
  • http://amt.ty.land.to/pukiwiki/pukiwiki.php?Chapter%20%207%20HTML%20Processing%20with%20Tokens
    • HTML::TokeParser関係
▲ ▼

日本語処理

  • http://openlab.jp/Jcode/index-j.html
    • Jcode.pm本家
  • http://www.hikoboshi.org/perl/doc/encode.html
    • Perl 5.8からはEncode.pmで処理
  • http://www.namazu.org/~tsuchiya/perl/perl-5.8.html
  • http://digit.que.ne.jp/work/index.cgi?Encode
▲ ▼

CPAN

  • http://www.fuji.sakura.ne.jp/%7Eyada/talk2000/perl.shtml
    • ついでに
  • http://www.cpan.jp/

}}

メニュー

  • トップ
  • 授業
  • PukiWiki Log
  • Install Log
  • 道具箱
  • セキュリティ情報
  • RSSアンテナ

大学関係リンク

  • Webメール
  • 健康システム学科
  • 情報メディアセンター
  • 兵庫大学

今日の5件
  • FrontPage(530)
  • Lecture/JouhouC2008/9th/exercise1(4)
  • Lecture/InfoPrac2004/2nd/1st(4)
  • Lecture/JouhouC2004(4)
  • Lecture/CompPracC2005(3)
最新の5件
2016-04-08
  • Lecture/timetable_2016
  • Lecture
  • FrontPage
2015-09-30
  • Lecture/timetable_2015
2015-04-04
  • MenuBar

total: 2037
today: 1
yesterday: 1
now: 6


リロード   差分   ホーム 一覧 検索 最終更新 バックアップ リンク元   ヘルプ   最終更新のRSS
http%3A%2F%2Fhs-www.hyogo-dai.ac.jp%2F~kawano%2F%3FPerl_de_RSS
Founded by Minoru Kawano.
Powered by PukiWiki Plus! 1.4.7plus-u2-i18n. HTML convert time to 0.206 sec.
Valid XHTML 1.1