Perlで単純な本文抽出をやってみる ~何も考えていない~
#!perl use strict; use warnings; use Dumpvalue; use LWP::UserAgent; my($html,$head,$body,$title,$content); print "content-type:text/html\n\n"; # ファイル取得(直接) #open my $FL, '<', 'source.html'; #$html = join('', <$FL>); #close $FL; # ファイル取得(UA経由) my $url = 'http://japanese.engadget.com/2009/03/27/automatic-pizza-machine/'; $html = LWP::UserAgent->new->request(HTTP::Request->new(GET => $url))->content; # 危険なやつは除去 $html =~ s/<(script|style|select|noscript)[^>]*>.*?<\/\1\s*>//gs; # 各開始タグ除去 $html =~ s/<(div|td|li|center).*?>//g; # ヘッダ、ボディー、タイトル抽出 if ($html =~ /<head>(.+?)<\/head>.*?<body.*?>(.+?)<\/body>/s) { $head = $1; $body = $2; if ($head =~ /<title>(.+?)<\/title>/) { $title = $1; } } # パラグラフ抽出 my @paragraph = split('<\/(div|td|li|center)>', $body); map { if (length($content) < length($_)) { $content = $_; } } @paragraph; print $content; #print Dumpvalue->new->dumpValue(\@paragraph);
段落ごとに区切って中に一番テキスト量のあるのがコンテンツとやってる。
閾値すら設けていない…!
再帰的に階層掘っていけばちゃんとしたのになりそう?
→よくあるパターンはdivとかで区切ってその中のテキストが本文っぽいのか判別してチェックしてるっぽ?
HTML::Extract::Contentほどがんばらなくても抽出できる方法ってないのかなー。