Perl Bloom::Filter与Web::Scraper
安装Bloom::Filter之前要先安装Digest::SHA1 #!/usr/bin/perl $bf=Bloom::Filter->new(capacity => 10,error_rate => .001); 创建一个Bloom::Filter,其最大容量为10,判断某一个元素是否在容器中的最大出错概率为0.001。 $bf->add(@keys); 向容器中添加元素。 $bf->check($_) 判断某一个元素是否在容器中。 <<===========================================================================>> 快速掠一下下面的代码,初次看是很难看懂。 use URI; Web::Scaper是一组用来提取HTML文档中元素内容的工具集,它能够理解HTML和CSS选择器以及XPath表达式。 $res = $scraper->scrape(URI->new($uri)); 如果你传入的参数是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 id="1">foo</li><li id="2">bar</li></ul># list => [ { id => "1",text => "foo" },{ id => "2",text => "bar" } ];process "li","list[]" => { id => '@id',text => "TEXT" }; #返回散列数组 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |