若是要提取 html 文件中的某些内容,该怎么做?直接用正则表达式。或者用解析 HTML 的模块 HTML::TokeParser::Simple 或者 HTML::Parser。嫌麻烦?可以试试看 Web::Scraper。比如:
my $res = scraper { process "div.message", message => 'TEXT' }->scrape($content);
文档中展示的用法,非常简练,和 jquery 一样符合直觉的操作方式:
use URI; use Web::Scraper; my $tweets = scraper { process "li.status", "tweets[]" => scraper { process ".entry-content", body => 'TEXT'; process ".entry-date", when => 'TEXT'; process 'a[rel="bookmark"]', link => '@href'; }; }; my $res = $tweets->scrape( URI->new("http://twitter.com/miyagawa") ); for my $tweet (@{$res->{tweets}}) { print "$tweet->{body} $tweet->{when} (link: $tweet->{link})\n"; }