php提取网页正文内容的例子__1_第1页
php提取网页正文内容的例子__1_第2页
php提取网页正文内容的例子__1_第3页
php提取网页正文内容的例子__1_第4页
php提取网页正文内容的例子__1_第5页
免费预览已结束,剩余9页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、php提取网页正文内容的例子_ 由于难点在于如何去识别并保留网页中的文章部分,而且删除其它无用的信息,并且要做到通用化,不能像火车头那样依据目标站来制定采集规章,由于搜索引擎结果中有各种的网页。 抓回一个页面的数据,如何匹配出正文部分,郑晓在下班路上想了个思路是: 1. 提取出body标签部分剔除全部链接剔除全部script、说明剔除全部空白标签(包括标签内不含中文的)猎取结果。 2. 挺直匹配出非链接的、 符合在div、p、h标签中的中文部分? 还是会有不少其它多余信息啊,比如底部信息等。 如何搞?不知道大家有木有什么思路或建议? 这个类是从网上找到的一个php实现的提取网页正文部分的算法,

2、郑晓在本地也测试了下,精准率特别高。 代码如下: ?php class readability / 保存判定结果的标记位名称 const attr_content_score = contentscore; / dom 解析类目前只支持 utf-8 编码 const dom_default_charset = utf-8; / 当判定失败时显示的内容 const message_can_not_get = readability was unable to parse this page for content.; / dom 解析类(php5 已内置) protected $dom = nu

3、ll; / 需要解析的源代码 protected $source = ; / 章节的父元素列表 private $parentnodes = array(); / 需要删除的标签 / note: added extra tags from private $junktags = array(style, form, iframe, script, button, input, textarea, noscript, select, option, object, applet, basefont, bgsound, blink, canvas, command, menu, nav, data

4、list, embed, frame, frameset, keygen, label, marquee, link); / 需要删除的属性 private $junkattrs = array(style, class, onclick, onmouseover, align, border, margin); /* * 构造函数 * param $input_char 字符串的编码。默认 utf-8,可以省略 */ function _construct($source, $input_char = utf-8) $this-source = $source; / dom 解析类只能处理

5、utf-8 格式的字符 $source = mb_convert_encoding($source, html-entities, $input_char); / 预处理 html 标签,剔除冗余的标签等 $source = $this-preparsource($source); / 生成 dom 解析类 $this-dom = new domdocument(1.0, $input_char); try /libxml_use_internal_errors(true); / 会有些错误信息,不过没关系 :) if (encoding=.readability:dom_default_ch

6、arset.$source) throw new exception(parse html error!); foreach ($this-dom-childnodes as $item) if ($item-nodetype = xml_pi_node) $this-dom-removechild($item); / remove hack / insert proper $this-dom-encoding = readability:dom_default_charset; catch (exception $e) / . /* * 预处理 html 标签,使其能够精准被 dom 解析类

7、处理 * * return string */ private function preparsource($string) / 剔除多余的 html 编码标记,避开解析出错 preg_match(/charset=(w|-+);?/, $string, $match); if (isset($match1) $string = preg_replace(/charset=(w|-+);?/, , $string, 1); / replace all doubled-up br tags with p tags, and remove fonts. $string = preg_replace

8、(/br/? rns*br/?/i, /pp, $string); $string = preg_replace(/?font*/i, , $string); / see / - from $string = preg_replace(#script(.*?)(.*?)/script#is, , $string); return trim($string); /* * 删除 dom 元素中全部的 $tagname 标签 * * return domdocument */ private function removejunktag($rootnode, $tagname) $tags = $r

9、ootnode-getelementsbytagname($tagname); /note: always index 0, because removing a tag removes it from the results as well. while($tag = $tags-item(0) $parentnode = $tag-parentnode; $parentnode-removechild($tag); return $rootnode; /* * 删除元素中全部不需要的属性 */ private function removejunkattr($rootnode, $attr

10、) $tags = $rootnode-getelementsbytagname(*); $i = 0; while($tag = $tags-item($i+) $tag-removeattribute($attr); return $rootnode; /* * 依据评分猎取页面主要内容的盒模型 * 判定算法来自: * 这里由郑晓博客转发 * return domnode */ private function gettopbox() / 获得页面全部的章节 $allparagraphs = $this-dom-getelementsbytagname(p); / study all th

11、e paragraphs and find the chunk that has the best score. / a score is determined by things like: number of ps, commas, special classes, etc. $i = 0; while($paragraph = $allparagraphs-item($i+) $parentnode = $paragraph-parentnode; $contentscore = intval($parentnode-getattribute(readability:attr_conte

12、nt_score); $classname = $parentnode-getattribute(class); $id = $parentnode-getattribute(id); / look for a special classname if (preg_match(/(comment|meta|footer|footnote)/i, $classname) $contentscore -= 50; else if(preg_match( /(|s)(post|hentry|entry-?(content|text|body)?|article-?(content|text|body

13、)?)(s|$)/i, $classname) $contentscore += 25; / look for a special id if (preg_match(/(comment|meta|footer|footnote)/i, $id) $contentscore -= 50; else if (preg_match( /(post|hentry|entry-?(content|text|body)?|article-?(content|text|body)?)$/i, $id) $contentscore += 25; / add a point for the paragraph

14、 found / add points for any commas within this paragraph if (strlen($paragraph-nodevalue) 10) $contentscore += strlen($paragraph-nodevalue); / 保存父元素的判定得分 $parentnode-setattribute(readability:attr_content_score, $contentscore); / 保存章节的父元素,以便下次快速猎取 array_push($this-parentnodes, $parentnode); $topbox =

15、 null; / assignment from index for performance. / see for ($i = 0, $len = sizeof($this-parentnodes); $i $len; $i+) $parentnode = $this-parentnodes$i; $contentscore = intval($parentnode-getattribute(readability:attr_content_score); $orgcontentscore = intval($topbox ? $topbox-getattribute(readability:

16、attr_content_score) : 0); if ($contentscore $contentscore $orgcontentscore) $topbox = $parentnode; / 此时,$topbox 应为已经判定后的页面内容主元素 return $topbox; /* * 猎取 html 页面标题 * * return string */ public function gettitle() $split_point = - ; $titlenodes = $this-dom-getelementsbytagname(title); if ($titlenodes-le

17、ngth $titlenode = $titlenodes-item(0) / see $title = trim($titlenode-nodevalue); $result = array_map(strrev, explode($split_point, strrev($title); return sizeof($result) 1 ? array_pop($result) : $title; return null; /* * get leading image url * * return string */ public function getleadimageurl($nod

18、e) $images = $node-getelementsbytagname(img); if ($images-length $leadimage = $images-item(0) return $leadimage-getattribute(src); return null; /* * 猎取页面的主要内容(readability 以后的内容) * * return array */ public function getcontent() if (!$this-dom) return false; / 猎取页面标题 $contenttitle = $this-gettitle(); / 猎取页面主内容 $contentbox = $this-gettopbox(); /check if we found a suitable top-box. if($contentbox = null) throw new runtimeexception(readability:message_can_not_get); / 复制内容到新的 domdocument $target = new domdocument; $target-appendchild($target-importnode($contentbox, true); / 删除不需要的标

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论