基于simple_html_dom的使用小结


Posted in PHP onJuly 01, 2013
<P>简单范例
<?phpinclude "simple_html_dom.php" ;    //加载simple_html_dom.php文件
$html = file_get_html('http://www.google.com/');  //获取html$dom = new simple_html_dom();    //new simple_html_dom对象$dom->load($html)      //加载html// Find all images foreach($dom->find('img') as $element) {   //获取img标签数组       echo $element->src . '<br>';    //获取每个img标签中的src}// Find all links foreach($dom->find('a') as $element){    //获取a标签的数组       echo $element->href . '<br>';    //获取每个a标签中的href}</P><P>
$html = file_get_html('http://slashdot.org/');   //获取html$dom = new simple_html_dom();    //new simple_html_dom对象$dom->load($html);     //加载html// Find all article blocksforeach($dom->find('div.article') as $article) {       $item['title']     = $article->find('div.title', 0)->plaintext; //plaintext 获取纯文本    $item['intro']    = $article->find('div.intro', 0)->plaintext;    $item['details'] = $article->find('div.details', 0)->plaintext;    $articles[] = $item;}print_r($articles);</P><P>}</P><P>
// Create DOM from string</P><P>$html = str_get_html('<div id="hello">Hello</div><div id="world">World</div>');
$dom = new simple_html_dom();     //new simple_html_dom对象</P><P>$dom->load($html);      //加载html
$dom->find('div', 1)->class = 'bar';    //class = 赋值 给第二个div的class赋值</P><P>$dom->find('div[id=hello]', 0)->innertext = 'foo';   //innertext内部文本</P><P>echo $dom; </P><P>// Output: <div id="hello">foo</div><div id="world" class="bar">World</div></P><P> </P><P>DOM methods & properties 
Name Description 
void __construct ( [string $filename] ) 构造函数,将文件名参数将自动加载内容,无论是文本或文件/ url。 
 string plaintext 纯文本 
void clear () 清理内存 
void load ( string $content ) 加载内容 
string save ( [string $filename] ) Dumps the internal DOM tree back into a string. If the $filename is set, result string will save to file. 
void load_file ( string $filename ) Load contents from a from a file or a URL. 
void set_callback ( string $function_name ) 设置一个回调函数。 
mixed find ( string $selector [, int $index] ) 找到元素的CSS选择器。返回第n个元素对象如果索引设置,否则返回一个数组对象。 </P>
<P> 4.find 方法详细介绍</P><P>
find ( string $selector [, int $index] ) 
// Find all anchors, returns a array of element objects a标签数组
$ret = $html->find('a');</P><P>// Find (N)th anchor, returns element object or null if not found (zero based)第一个a标签
$ret = $html->find('a', 0);</P><P>// Find lastest anchor, returns element object or null if not found (zero based)最后一个a标签
$ret = $html->find('a', -1); </P><P>// Find all <div> with the id attribute 
$ret = $html->find('div[id]');</P><P>// Find all <div> which attribute id=foo
$ret = $html->find('div[id=foo]'); </P><P>
// Find all element which id=foo
$ret = $html->find('#foo');</P><P>// Find all element which class=foo
$ret = $html->find('.foo');</P><P>// Find all element has attribute id
$ret = $html->find('*[id]'); </P><P>// Find all anchors and images a标签与img标签数组 
$ret = $html->find('a, img');  </P><P>// Find all anchors and images with the "title" attribute
$ret = $html->find('a[title], img[title]');</P><P>
// Find all <li> in <ul> 
$es = $html->find('ul li'); ul标签下的li标签数组</P><P>// Find Nested <div> tags
$es = $html->find('div div div');  div标签下div标签下div标签数组</P><P>// Find all <td> in <table> which class=hello 
$es = $html->find('table.hello td'); table标签下td标签数组</P><P>// Find all td tags with attribite align=center in table tags 
$es = $html->find(''table td[align=center]'); </P><P> 5.Element  的方法
$e = $html->find("div", 0);                              //$e 所拥有的方法如下表所示
Attribute Name Usage 
$e->tag 标签 
$e->outertext 外文本 
$e->innertext 内文本 
$e->plaintext 纯文本 </P><P> </P><P>// Example
$html = str_get_html("<div>foo <b>bar</b></div>"); 
echo $e->tag; // Returns: " div"
echo $e->outertext; // Returns: " <div>foo <b>bar</b></div>"
echo $e->innertext; // Returns: " foo <b>bar</b>"
echo $e->plaintext; // Returns: " foo bar"</P><P>6.DOM traversing 方法
Method Description 
mixed$e->children ( [int $index] ) 子元素 
element$e->parent () 父元素 
element$e->first_child () 第一个子元素 
element$e->last_child () 最后一个子元素 
element$e->next_sibling () 后一个兄弟元素 
element$e->prev_sibling () 前一个兄弟元素 </P><P>
// Example
echo $html->find("#div1", 0)->children(1)->children(1)->children(2)->id;
// or 
echo $html->getElementById("div1")->childNodes(1)->childNodes(1)->childNodes(2)->getAttribute('id');
</P>
PHP 相关文章推荐
BBS(php &amp; mysql)完整版(二)
Oct 09 PHP
php 文本文件的读取效率
Feb 10 PHP
discuz目录文件资料汇总
Dec 30 PHP
php数字运算验证码的实现代码
Jul 30 PHP
Yii CGridView用法实例详解
Jul 12 PHP
PHP常用操作类之通信数据封装类的实现
Jul 16 PHP
PHP mysqli事务操作常用方法分析
Jul 22 PHP
php使用 readfile() 函数设置文件大小大小的方法
Aug 11 PHP
详解php中curl返回false的解决办法
Mar 18 PHP
PHP基础之输出缓冲区基本概念、原理分析
Jun 19 PHP
PHP常用函数之获取汉字首字母功能示例
Oct 21 PHP
PHP实现基本留言板功能原理与步骤详解
Mar 26 PHP
解析php php_openssl.dll的作用
Jul 01 #PHP
关于php 接口问题(php接口主要也就是运用curl,curl函数)
Jul 01 #PHP
浅析PKI加密解密 OpenSSL
Jul 01 #PHP
php pki加密技术(openssl)详解
Jul 01 #PHP
使用php实现快钱支付功能(涉及到接口)
Jul 01 #PHP
在wamp集成环境下升级php版本(实现方法)
Jul 01 #PHP
mongo Table类文件 获取MongoCursor(游标)的实现方法分析
Jul 01 #PHP
You might like
php gd2 上传图片/文字水印/图片水印/等比例缩略图/实现代码
2010/05/15 PHP
PHP 图像尺寸调整代码
2010/05/26 PHP
基于PHP文件操作的详细诠释
2013/06/21 PHP
ThinkPHP中自定义目录结构的设置方法
2014/08/15 PHP
Zend Framework教程之Zend_Config_Ini用法分析
2016/03/23 PHP
php gd等比例缩放压缩图片函数
2016/06/12 PHP
js 实现css风格选择器(压缩后2KB)
2012/01/12 Javascript
node.js中的fs.fchmod方法使用说明
2014/12/16 Javascript
javascript实现带下拉子菜单的导航菜单效果
2015/05/14 Javascript
JQuery中模拟image的ajaxPrefilter与ajaxTransport处理
2015/06/19 Javascript
jQuery UI仿淘宝搜索下拉列表功能
2017/01/10 Javascript
Javascript Event(事件)的传播与冒泡
2017/01/23 Javascript
Jil,高效的json序列化和反序列化库
2017/02/15 Javascript
基于JQuery和原生JavaScript实现网页定位导航特效
2017/04/03 jQuery
vue 页面回退mounted函数不执行的解决方案
2020/07/26 Javascript
[01:20]DOTA2 2017国际邀请赛冠军之路无止竞
2017/06/19 DOTA
[54:26]完美世界DOTA2联赛PWL S3 Forest vs Rebirth 第一场 12.10
2020/12/12 DOTA
Python环境下搭建属于自己的pip源的教程
2016/05/05 Python
python中文件变化监控示例(watchdog)
2017/10/16 Python
Python探索之爬取电商售卖信息代码示例
2017/10/27 Python
Python3实现的简单三级菜单功能示例
2019/03/12 Python
OpenCV-Python 摄像头实时检测人脸代码实例
2019/04/30 Python
Python3.5文件读与写操作经典实例详解
2019/05/01 Python
Python Web静态服务器非堵塞模式实现方法示例
2019/11/21 Python
python GUI库图形界面开发之PyQt5下拉列表框控件QComboBox详细使用方法与实例
2020/02/27 Python
pycharm 多行批量缩进和反向缩进快捷键介绍
2021/01/15 Python
python中用Scrapy实现定时爬虫的实例讲解
2021/01/18 Python
HTML5之SVG 2D入门2—图形绘制(基本形状)介绍及使用
2013/01/30 HTML / CSS
好药师网上药店:安全合法的网上药品零售药房
2017/02/15 全球购物
美国球鞋寄卖网站:Stadium Goods
2018/05/09 全球购物
美国鲜花递送:UrbanStems
2021/01/04 全球购物
党校学习党性分析材料
2014/12/19 职场文书
2015年实习班主任工作总结
2015/04/23 职场文书
小学生安全教育心得体会
2016/01/15 职场文书
创业者如何撰写出一份打动投资人的商业计划书?
2019/07/02 职场文书
MySQL派生表联表查询实战过程
2022/03/20 MySQL