ExtractContentで本文抽出を試す。
http://d.hatena.ne.jp/naoya/20090911
ここで読んだはてなの本文抽出で使用されているという
ExtractContent
WEBの本文をperlを使って、抜き出すライブラリらしい。
これまで自分のサイトなどでは、simple_html_dom.php
を使って、HTMLをパースしたものを正規表現で色々やっていたので、
こんな便利なものがあるなら!使ってみたい!とさっそく試す。
http://perl-mongers.org/2008/10/extract-content-from-html.html
初perlなので、cpanなど使い慣れず、四苦八苦したけど
最終的には下記手順で無事出来ました。
1.CPANのインストール
1-1下記のような質問にすべて、エンターでデフォルトを指定。
Are you ready for manual configuration? [yes] CPAN build and cache directory? [/root/.cpan] Cache size for build directory (in MB)? [10] Perform cache scanning (atstart or never)? [atstart] Cache metadata (yes/no)? [yes] Your terminal expects ISO-8859-1 (yes/no)? [yes] File to save your history? [/root/.cpan/histfile] Number of lines to save? [100] Policy on building prerequisites (follow, ask or ignore)? [ask] Where is your gzip program? [/bin/gzip] Where is your tar program? [/bin/tar] Where is your unzip program? [/usr/bin/unzip] Where is your make program? [/usr/bin/make] Where is your links program? [] Warning: links not found in PATH Where is your links program? [] Where is your wget program? [/usr/bin/wget] Warning: ncftpget not found in PATH Where is your ncftpget program? [] Warning: ncftp not found in PATH Where is your ncftp program? [] Where is your ftp program? [/usr/bin/ftp] Where is your gpg program? [/usr/bin/gpg] What is your favorite pager program? [/usr/bin/less] What is your favorite shell? [/bin/bash] [f:id:oggata:20100909190620j:image]
1-3
インストール完了。exitやquitでcpanから抜ける
3.HTML-ParseのHTML::Entitesをcpanでインストール
http://search.cpan.org/dist/HTML-Parser/lib/HTML/Entities.pm
4.HTML::ExtractContentをcpanでインストール
http://perl-mongers.org/2008/10/extract-content-from-html.html
cpan install HTML::ExtractContent
(必要なモジュールをインストールするか確認が何度か出るので随時エンター)
/usr/bin/make install -- OKとなったら〇
5.テスト用のperlを作成
5-1.viで.plファイルを作成
vi testmodule.pl
5-2.サンプル下記
------------------------------------ #!/usr/bin/perl use strict; use warnings; use HTML::ExtractContent; use LWP::UserAgent; my $agent = LWP::UserAgent->new; my $res = $agent->get('http://news.livedoor.com/article/detail/4999438/'); my $extractor = HTML::ExtractContent->new; $extractor->extract($res->decoded_content); print $extractor->as_text; ------------------------------------
5-3.実行権限を付与
chmod 777 ./testmodule.pl
6.実行してみる
./testmodule.pl
7.エラーが起きる
Can't locate LWP/UserAgent.pm in @INC (@INC contains: /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi
8.UserAgentを入れる
cpan install LWP::UserAgent
(必要なモジュールをインストールするか確認が何度か出るので随時エンター)
Appending installation info to /usr/lib/perl5/5.8.8/i386-linux-thread-multi/perllocal.pod
/usr/bin/make install -- OK
となれば〇
11.反省点
一連の流れを綺麗に通すまで
かなり時間がかかったので反省点をいくつか
11-0.Can't locate HTML/ExtractContent.pm in @INC (@INC contains: などのエラーにびびらない
こういう場合は cpan install HTML::ExtractContentとかってやるとインストールされる。
エラーをどんどん絞っていこう。
11-1.Can't locate HTML/Entities.pm エラー
HTML::EntitesはHTML-Parseをインストールすると一緒にインストールされる
単体でEntitiesをいれて(cpan install HTML::Entites)いたが、
http://search.cpan.org/dist/HTML-Parser/lib/HTML/Entities.pm
を読むと、親にあたるHTML-Parseを入れれば一緒に入るらしい
11-2./bin/sh: gcc: command not found make: *** [Parser.o] Error 127
GAAS/HTML-Parser-3.67.tar.gz : make NO
この場合、gccがインストールされていないため起る。
結果HTML::Parserがインストールできないという事態に陥るので忘れずにyum install gcc。
11-4.find `perl -e 'print "@INC"'` -name '*.pm' -print
でCPANがはいったか確認できるらしい
11-5.本文抽出には他にも色々あるらしい
HTML::Feature
HTML::ExtractContent
11-6.依存関連で自動的にインストールしてくれたけれど
HTML/TreeBuilderに依存しているので自分でインストールがひつような場合もあるらしい
11-7.ExtractorContentをPHPでやるにはopenpearでインストールできるようでできないみたい
まずpearをインストールして、openpearもインストール。
yum install php
yum install php-pear
pear channel-discover openpear.or
pear install openpear/HTML_ExtractContent
とやってみるも見つからない。原因は、まだ未リリースらしい。がーん。
http://openpear.org/repository/HTML_ExtractContent/trunk/tests/HTML/ExtractContent/ExtractContentTest.php