Web::Scraperを使ってmuxtapeのタイトルを取得する?

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

perl初心者のまめこです

こんなに場違いなところで記事を書くのは初めてです!


tomyheroからの課題で muxtapeのタイトルを取得して表示させるスクリプトを書いてみましたー。ほげほげ。

正解コードはこちら

#!/usr/local/bin/perl

use strict;
use Web::Scraper;
use LWP::UserAgent;
use Perl6::Say;
use Data::Dump;

my $user = $ARGV[0] || 'woopsdez' ;
my $web = scraper{
        process '//div[@class="name"]',
                'titles[]' => 'TEXT',
};

my $ua = LWP::UserAgent->new( agent =>'Mozilla/5.0 (Macintoch; U; Intel Mac OS X; ja-JP-mac; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14' );
$web->user_agent( $ua );
my $response = $web->scrape( URI->new( "http://$user.muxtape.com/") );

for my $title ( @{$response->{titles}} ){
        say $title;
}

まめの書いた間違いコードはこちら

#!/usr/local/bin/perl

use strict;
use Web::Scraper;
use URI;
use Perl6::Say;
use Data::Dump;

my $scraper = scraper {
        process '#onge5ad938d2abd7574eb1900542c5a4a21 .name','title' => 'TEXT';
};

my $uri = new URI('http://woopsdez.muxtape.com/');

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

say dump($res->{title});

print $res->{title};

参考にしたところ

use Web::Scraper; - 今日のCPANモジュール

つまづいたところ

Web::Scraperっていうので

process ' cssのid or class名 ' , '自分の好きな名前' => 'TEXT';

ってやれば$scraperに指定したidやclassの中身のテキストが取得出来るんだー。と簡単に考えてたけど上手く取得出来てないみたいですた。

dumpをしてみても[Abort trap]って出るばかり。なんだったんだろう。


先生の言うとおり書いてみたら動いた!

試しに書きかえてFlickrの写真タイトルを取得しようとやってみたけど失敗しました。


むー。(´・ω・`)


次はyusukebeさんが作ったWebService::Simple;でやってみたいと思うます!

No TrackBacks

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

2 Comments

Leave a comment

About this Entry

This page contains a single entry by woopsdez published on May 30, 2008 9:16 PM.

Catalyst からのメール送信 was the previous entry in this blog.

実用! Perlで法律改正日を調べる 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.2rc1-en