当前位置:编程学习 > php >>

PHP用DOM方式处理HTML之《Simple HTML DOM》

入门级

simple_html_dom插件
用dom处理html文件的利器
使用:
加载simple_html_dom.php文件

require_once 'simple_html_dom.php'

new simple_html_dom对象

$dom = new simple_html_dom()

加载html

$dom->load($html);

find()方法

$dom->find('div.lookLeftname', 0)->plaintext

class=‘lookLeftname'的div中的纯文本

$dom->find('div.lookLeftname', 0)->innertext

class='lookLeftname'的div中的内部文本

完整测试实例

 代码如下 复制代码

<?php
require_once("simple_html_dom.php");
ini_set('memory_limit','1000M');
ini_set("max_execution_time",6000000);

for($i=1;$i<=21;$i++){
 $html = file_get_html(http://www.zzzyk.com);
 $as = $html->find('.shopname a');
 $sum = 0;
 foreach($as as $a) {
  if($a->plaintext != "分店"){
   $a->href = "http://www.xx.com".$a->href;
   echo $a->outertext."------".$a->href."-------<br>";
   $sum++;
  }
 }
 echo "<br><br>第 {$i} 页结束 ,数目:{$sum}<hr>";
}
?>

补充:Php教程,XML应用 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,