*PerlでRSS [#r2de88cd]
Perlを使って、静的なHTMLファイルから、RSSデータを生成したい。
某授業のため。
**HTMLファイルの一覧をスキャン [#q2c0aa2f]
あるディレクトリを再帰的に見る、のを書く予定。ファイルシステムからにするか、区ローラーっぽくするか…
**HTML->RSSの生成 [#f20269bd]
とりあえずこんな感じ。
Template::Extactも試してみたけど、フォーマットがきっちりしてないと、
うまくHTMLファイルから情報を拾えないみたい。
結局、HTML::TokeParserで無難に処理。
#pre(novervatim){{
#!/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");
}
}}
**情報収集 [#vec74b58]
***Perl入門 [#g89a3022]
-http://jp.rubyist.net/PerlMa/wiki.cgi?page=20060401-FirstStepPerl
--メタ情報
-http://www.kiwi-us.com/~mizusawa/penguin/html_hint/perlscriptbox.html
--Tips集
***RSS関係 [#pe0c7457]
-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からインストールできず -- &new{2006-08-22 (火) 15:20:45};
- 結局、Vineのapt-getからインストール…(apt-get install perl-XML-RSS) -- &new{2006-08-22 (火) 15:21:19};
- XXebkUlcX -- [[idcsjumziqw]] &new{2011-04-03 (日) 21:42:48};
#comment
***タグ抽出 [#se694ead]
-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関係
***日本語処理 [#p1d9f735]
-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 [#k19269b2]
-http://www.fuji.sakura.ne.jp/%7Eyada/talk2000/perl.shtml
--ついでに
-http://www.cpan.jp/
}}
|