Drkcore

21 07 2007 perl Tweet

HTML::Selector::XPath::Simple

他のサイトをゴニョゴニョやって欲しいデータだけ抜き出すのに、Template::Extractとか正規表現つかったりするわけですが、今回HTML::Selector::XPathを使ってみた。

その流れでcpanサーチしてた時に、Simpleってのを見つけたが、これはHTML::TreeBuilder::XPathでHTMLをパースし HTML::Selector::XPathのCSS セレクタをXPath に変換して、ノードを探すってのをやってくれるげなモジュール。

というわけで、Hatena::Diary::Naoyaの例だと、

use strict;
use warnings;

use URI::Fetch;
use HTML::Selector::XPath::Simple;

my $id = shift or die "usage: $0 <id>";
my $res = URI::Fetch->fetch(sprintf 'http://b.hatena.ne.jp/%s/', $id)
    or die URI::Fetch->errstr;

my $selector = HTML::Selector::XPath::Simple->new($res->content);
my @nodes = $selector->select('ul.bookmarkinfo li.favorited');

printf "User '%s' is favorited from %d users\n", $id, $nodes[1];

で書けた、シンプル。

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021