基于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 相关文章推荐
PHP程序61条面向对象分析设计的经验小结
Nov 12 PHP
判断PHP数组是否为空的代码
Sep 08 PHP
利用php实现禁用IE和火狐的缓存问题
Dec 03 PHP
利用php获取服务器时间的实现代码
Jun 07 PHP
基于PHPExcel的常用方法总结
Jun 13 PHP
Zend studio文件注释模板设置方法
Sep 29 PHP
php脚本运行时的超时机制详解
Feb 17 PHP
PHP strcmp()和strcasecmp()的区别实例
Nov 05 PHP
php修改数组键名的方法示例
Apr 15 PHP
PHP常用日期加减计算方法实例小结
Jul 31 PHP
PHP大文件切割上传功能实例分析
Jul 01 PHP
php进行md5加密简单实例方法
Sep 19 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显示MySQL数据的三种方法
2008/06/05 PHP
基于PHP CURL用法的深入分析
2013/06/09 PHP
百万级别知乎用户数据抓取与分析之PHP开发
2015/09/28 PHP
PHP实现发送邮件的方法(基于简单邮件发送类)
2015/12/17 PHP
PHP封装的PDO数据库操作类实例
2017/06/21 PHP
js设置cookie过期及清除浏览器对应名称的cookie
2013/10/24 Javascript
javascript框架设计读书笔记之字符串的扩展和修复
2014/12/02 Javascript
一步一步封装自己的HtmlHelper组件BootstrapHelper(三)
2016/09/14 Javascript
通过npm引用的vue组件使用详解
2017/03/02 Javascript
Vue响应式原理详解
2017/04/18 Javascript
微信小程序request请求后台接口php的实例详解
2017/09/20 Javascript
element-ui 限制日期选择的方法(datepicker)
2018/05/16 Javascript
vue2.0自定义指令示例代码详解
2019/04/25 Javascript
VUE单页面切换动画代码(全网最好的切换效果)
2019/10/31 Javascript
vue中实现点击按钮滚动到页面对应位置的方法(使用c3平滑属性实现)
2019/12/29 Javascript
vue 实现在同一界面实现组件的动态添加和删除功能
2020/06/16 Javascript
python设置检查点简单实现代码
2014/07/01 Python
总结python实现父类调用两种方法的不同
2017/01/15 Python
[原创]使用豆瓣提供的国内pypi源
2017/07/02 Python
Python学习_几种存取xls/xlsx文件的方法总结
2018/05/03 Python
Python开发的十个小贴士和技巧及长常犯错误
2018/09/27 Python
python 判断linux进程,并杀死进程的实现方法
2019/07/01 Python
详解python实现数据归一化处理的方式:(0,1)标准化
2019/07/17 Python
浅谈Python中(&amp;,|)和(and,or)之间的区别
2019/08/07 Python
有关HTML5中背景音乐的自动播放功能
2017/10/16 HTML / CSS
DNA基因检测和分析:23andMe
2019/05/01 全球购物
员工拾金不昧表扬信
2014/01/09 职场文书
公证委托书模板
2014/04/03 职场文书
档案工作汇报材料
2014/08/21 职场文书
2014年教师节红领巾广播稿
2014/09/10 职场文书
教师党员个人整改措施材料
2014/09/16 职场文书
中学生打架检讨书
2014/10/13 职场文书
2015年党员个人剖析材料
2014/12/18 职场文书
新闻稿格式范文
2015/07/18 职场文书
中秋节英文祝福语句(14句)
2019/09/11 职场文书
Apache POI操作批量导入MySQL数据库
2022/06/21 Servers