muxtapeのタイトルをRSSから取得する

| 0 Comments | 0 TrackBacks | このエントリーをはてなブックマークに追加 このエントリーのはてなブックマーク件数

まめこさんが「Web::Scraperを使ってmuxtapeのタイトルを取得する?」というエントリーを投稿しましたね。

http://perl-mongers.org/2008/05/webscrapermuxtape.html


おそらく Web::Scraper の勉強のために、ページをスクレイピングしてますが、

muxtape は RSSフィードを吐いているため RSS をパースすればもっとシンプルに記述できます。


今回は

  • XML::Feed
  • XML::RSS
  • WebService::Simple

の3つのモジュールをそれぞれ使って、タイトルを print するサンプルコードを書いてみました。なんか まめこさんのmuxtapeだと日本語が文字化けているので asano さんの muxtape から取得してみます。

XML::Feed 版
use strict;
use warnings;
use XML::Feed;

my $user = $ARGV[0] || "asano";
my $feed = XML::Feed->parse( URI->new("http://$user.muxtape.com/rss") );
for my $entry ( $feed->entries ) {
    print $entry->title . "\n";
}

XML::RSS 版
use strict;
use warnings;
use XML::RSS;
use LWP::Simple;

my $user    = $ARGV[0] || "asano";
my $content = get("http://$user.muxtape.com/rss");
my $rss     = XML::RSS->new;
$rss->parse($content);
foreach my $item ( @{ $rss->{items} } ) {
    print $item->{title} . "\n";
}

WebService::Simple 版
use strict;
use warnings;
use WebService::Simple;

my $user = $ARGV[0] || "asano";
my $ws  = WebService::Simple->new( base_url => "http://$user.muxtape.com/rss" );
my $res = $ws->get;
my $ref = $res->parse_response;
foreach my $item ( @{ $ref->{channel}->{item} } ) {
    print $item->{title} . "\n";
}

他にもRSSもしくはXMLのパーサーのモジュールがあるので、他にもやり方がいろいろありますね。用途にもよりますが個人的には、XML::Feed を使うのがいいのかなと思います。皆さんはどんな感じでしょうかね。これまたツッコミ歓迎です!

No TrackBacks

TrackBack URL: http://perl-mongers.org/MT/mt-tb.cgi/35

Leave a comment

About this Entry

This page contains a single entry by yusukebe published on May 31, 2008 12:10 PM.

実用! Perlで法律改正日を調べる was the previous entry in this blog.

Mooseに入門してみたよ is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Categories

Pages

Creative Commons License
This blog is licensed under a Creative Commons License.
Powered by Movable Type 4.21-en