教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php html解析器Simple HTML Dom使用说明

php html解析器Simple HTML Dom使用说明

发布时间:2016-10-26   编辑:jiaochengji.com
教程集为您提供php html解析器Simple HTML Dom使用说明等资源,欢迎您收藏本站,我们将为您提供最新的php html解析器Simple HTML Dom使用说明资源
本文章来给大家介绍一下关于Simple HTML Dom解析器的使用方法详解,有需要了解的同学不防进入参考。

 

1. 开始使用

首先下载解压缩,然后将simple_html_dom.php文件包含进要编写的脚本文件中,加载要处理的html,支持三种模式的html加载,分别是『从url中加载,从字符串中加载,从文件中加载』。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2568')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2568>

<?php
require_once('simple_html_dom.php');
//从url加载
$html = file_get_html('http://www.jiaochengji.com');
//从字符串加载
$html = str_get_html('<html><body>Hello World!</body></html>');
//从文件中加载
$html = file_get_html('example.htm');
从字符串加载网上文件需要先从网络下下载,使用cURL比较好一些,需要在php配置文件中打开php扩展php_curl。

$url = 'http://www.jiaochengji.com';
$ci = curl_init();
curl_setopt($ci,CURLOPT_URL,$url);
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);

2. 查找html元素
使用find函数查找,返回包含对象的数组,常见的查找如下。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6467')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6467>//查找超链接元素
$alink = $html->find('a');
//查找第n个连接元素
$alink = $html->find('a',5);
//查找id为main的div
$mainDiv = $html->find('div[id=main]');
//查找所有定义了id的div
$idDiv = $html->find('div[id]');
//查找所有定义了id的元素
$idAll = $html->find('[id]');
//查找样式类为info的元素
$classInfo = $html->find('.info');
//支持嵌套子元素查找
$ret = $html->find('ul li');
//查找多个html元素
$ret = $html->find('a,img,p');
//....

3. 其他
可以使用内置的函数来进行元素的定位,返回父元素parent,返回子元素数组children,返回第一个子元素first_child,返回最后一个子元素last_child,返回前一个相邻元素prev_sibling,返回后一个相邻元素next_sibling等。

提供简单的正则表达式来过滤属性选择器,类似于[attribute]的格式。

每个对象都有4个基本属性:
tag — 返回html标签名
innertext — 返回innerHTML
outertext — 返回outerHTML
plaintext — 返回HTML标签中的文本

返回元素属性值

//返回$alink的href值
$link = $alink->href;
通过设置元素的属性值可以对元素进行添加、修改、删除操作。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5205')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5205>

//删除url连接
$alink->href = null;
//元素的修改
$ret->outertext = '<div class="nav">' . $ret->outertext . '</div>';
$ret->outertext = '';
$ret->outertext = $ret->outertext . '<div>other</div>';
$ret->outertext = '<div>Welcome</div>' . $ret->outertext;
-EOF-

您可能感兴趣的文章:
php html解析器Simple HTML Dom使用说明
python 解析xml需要什么模块
jQuery对象和DOM对象使用说明
Python中一些包的基本用处和安装方法
jQuery学习笔记[1] jQuery中的DOM操作
BOM与DOM的区别分析
python的爬虫是什么意思
php在web开发中如何使用
jQuery Simple Tooltips
php 获取标签之间文本的实现代码

[关闭]
~ ~