WWW::Mechanize::Plugin::Web::Scraperでスクレイピングをもっと簡単に

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

スクレイピングして何すんだと言われましても、スクレイピングがしたくてしょうがないmattnです。


今日、WWW::Mechanize::Plugin::Web::Scraperというcpanモジュールを(otsuneさんのブクマ経由で)見つけました。モジュール名の通り、WWW::MechanizeからWeb::Scraperするプラグインです。

先日書いた「何時でも何処でも携帯で「はてなスター」チェック」では、両方使ってagentを切り替えたりしてましたが、これを使うともっとスマートに書けるようになります。今日は小ネタでソースだけ。

use strict;
use warnings;
use WWW::Mechanize::Pluggable;
use YAML::Syck;

my $username = 'xxxxx';
my $password = 'xxxxx';

my $mech = WWW::Mechanize::Pluggable->new();
$mech->get('http://www.hatena.ne.jp/login?location=http://s.hatena.ne.jp/');
$mech->submit_form(
    form_number => 1,
    fields => {
        name     => $username,
        password => $password,
    }
);
$mech->get("http://s.hatena.ne.jp/$username/report");
my $stars = $mech->scrape('span.entry-title a', 'stars[]',
    { title => 'TEXT', link => '@href' }
);
warn Dump $stars;

いい感じにキレイですね。

No TrackBacks

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

Leave a comment

About this Entry

This page contains a single entry by mattn published on July 30, 2008 10:19 PM.

CGI.pmのファイルアップローダ was the previous entry in this blog.

PHP使いによるCatalyst初心者記事 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