File::FindとHTML::Parser
File::FindとHTML::Parserを使ってみた。備忘のためのメモを。
File::Find
指定ディレクトリ配下を再帰的に探索して、発見したらコールバック関数を呼ぶ。
といったところ。
use File::Find; find(\&findFile, "/home/hoge"); sub findFile{ my $path; #ディレクトリ名 if ($File::Find::dir =~ /\/$/) { $path .= $File::Find::dir; } else { $path .= $File::Find::dir."/"; } #ファイル名 $path .= $_; #何らかの処理 &doSomething($path); }
※http://d.hatena.ne.jp/perlcodesample/20080530/1212291182
を参考にした。
HTML::Parser
文字どおりHTMLを解析してくれる。
イベントドリブン。
my @parsed = (); &parseHTML($html); #httpまたはhttpsからはじまるURLのみがprintされる print @parsed, "\n\n"; sub parseHTML { my $p = HTML::Parser->new( api_version => 3, start_h => [ sub { my ($tagname, $attr, $text) = @_; if ($tagname eq 'a') { my $href = $attr->{href}; if ($href =~ /https?:/) { push(@parsed, $href); } } }, "tagname, attr, text"], marked_sections => 1, ); $p->parse(shift); }