Treebuilder utf8対策済み
#!/usr/bin/env perl use strict; use warnings; use utf8; use Encode; use HTML::TreeBuilder; binmode STDOUT, ':encoding(cp932)'; binmode STDERR, ':encoding(cp932)'; #use encoding "cp932"; my $html=<<'HTML'; <html> <head> <title>Test Page</title> </head> <body> <div id="content"> <h1>Test Page</h1> <h2>主なトピックス</h2> <ul class="clr"> <li><span class="dateRight">8月30日</span><a href="/topics/title1.html">Title1<\ /a></li> <li><span class="dateRight">8月29日</span><a href="/topics/title2.html">Title2<\ /a></li> <li><span class="dateRight">8月28日</span><a href="/topics/title3.html" class="\ newTitle">Title3</a></li> <li><span class="dateRight">8月27日</span><a href="/topics/title4.html">Title4<\ /a></li> </ul> </div> </body> </html> HTML
#ヒアドキュメントのEOD行には改行のみ入力できる。タブもスペースも入れちゃダメ!!
my $tree = HTML::TreeBuilder->new; $tree->parse($html); my @items = $tree ->look_down('id', 'content') ->look_down('class', 'clr') ->find('li'); for @items{ print $_->as_text."\n" ; } # 8月30日Title1 # 8月29日Title2 # 8月28日Title3 # 8月27日Title4