• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Perl Bloom::Filter与Web::Scraper

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

安装Bloom::Filter之前要先安装Digest::SHA1

#!/usr/bin/perl
use Bloom::Filter;
my $bf=Bloom::Filter->new(capacity => 10,error_rate => .001);
my @keys=qw/a s d f g h j k l z/;
$bf->add(@keys);
while(<>){
    chomp;
    print "Found $_\n" if $bf->check($_);
}

$bf=Bloom::Filter->new(capacity => 10,error_rate => .001);

创建一个Bloom::Filter,其最大容量为10,判断某一个元素是否在容器中的最大出错概率为0.001。

$bf->add(@keys); 向容器中添加元素。

$bf->check($_)

判断某一个元素是否在容器中。

<<===========================================================================>>

快速掠一下下面的代码,初次看是很难看懂。

use URI;
use Web::Scraper;

# First, create your scraper block
my $tweets = scraper {
     # Parse all LIs with the class "status", store them into a resulting
     # array 'tweets'.  We embed another scraper for each tweet.
     process "li.status", "tweets[]" => scraper {
         # And, in that array, pull in the elementy with the class
         # "entry-content", "entry-date" and the link
         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") );

 # The result has the populated tweets array
 for my $tweet (@{$res->{tweets}}) {
     print "$tweet->{body} $tweet->{when} (link: $tweet->{link})\n";
 }

The structure would resemble this (visually)
 {
   tweets => [
     { body => $body, when => $date, link => $uri },
     { body => $body, when => $date, link => $uri },
   ]
 }

Web::Scaper是一组用来提取HTML文档中元素内容的工具集,它能够理解HTML和CSS选择器以及XPath表达式。

$res = $scraper->scrape(URI->new($uri));
$res = $scraper->scrape($html_content); 
$res = $scraper->scrape(\$html_content); 
$res = $scraper->scrape($http_response); 
$res = $scraper->scrape($html_element);

如果你传入的参数是URI或HTTP response,那Web::Scaper自动去寻找Content-Type header和META标签以判断文件编码。否则你压根先把HTML内容解码为Unicode后再传给scape函数。

当你把HTML内容作为参数传给scrape函数时,你还要考虑一个问题:HTML文档中出现 相对路径怎么办?所以这个时候你可以把base url一并作为参数传进去。

$res = $scraper->scrape($html_content, "http://example.com/foo");

process函数用来根据给定的HTML或CSS选择器或XPath表达式来匹配元素,把text或attributes抽取到result中。

scraper { process "tag.class",key => 'TEXT';

    process '//tag[contains(@foo, "bar")]', key2 => '@attr';

};

它有两个参数,当第一个参数以"//"或"id("开头时作为XPath对待;否则作为HTML或CSS选择器对待。

# <span class="date">2008/12/21</span>
# date => "2008/12/21"
process ".date", date => 'TEXT';    # CSS的class是date

# <div class="body"><a href="http://example.com/">foo</a></div>
# link => URI->new("http://example.com/")
process ".body > a", link => '@href';    # href是个属性

# <div class="body"><a href="http://example.com/">foo</a></div>
# link => URI->new("http://example.com/"), text => "foo"
process ".body > a", link => '@href', text => 'TEXT';  # TEXT指标签内真正的内容

# <ul><li>foo</li><li>bar</li></ul>
# list => [ "foo", "bar" ]
process "li", "list[]" => "TEXT";    # []返回数组

# <ul><li };    #返回散列数组


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Perl 条件语句 - 长颈鹿的脖子发布时间:2022-07-22
下一篇:
perl特殊符号发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap