simple html dom img,simple_html_dom学习过程(1)查找元素
simplie_html_dom類?可以通過find方法 或者類似 getElementxxxx的方式來查找一個元素。
(1),利用find方法查找元素
首先引入 simple_html_dom.php類。并實例化
$sdom = new simple_html_dom(‘http://www.baidu.com’);
$sdom->find(‘a’) ?查找百度首頁的所有a標簽。結果返回的是一個對象數組。
查看simple_html_dom 源碼可以看到 find接受三個參數$selector, $idx, $lowercase
第二個為返回結果的第幾個;
找到find方法可以看到如圖:
第三個參數為:是否對大小寫敏感;
搜索 seek方法
第一個參數為選擇器
$sdom->find(‘a’) 查找所有a標簽(對象)
$sdom-find(‘a’,0) 返回第一個a標簽(對象)
$sdom->find(‘a’,-1) 返回最后一個a標簽(對象)
除了這種基本的查找方法,還可以用類似jquery中的
$sdom->find(‘#isDebugInfo‘); 查找所有ID為isDebugInfo的元素
$sdom->fnd(‘.isDebugInfo‘) ;查找所有class 為isDebugInfo的元素
$sdom->find(*[id]) ?查找具有id屬性的元素
$sdom->find(div[id=isDebugInfo]) 查找帶有id為isDebugInfo的div標簽
支持以下屬性選擇器操作:
過濾
描述
[attribute]
匹配具有指定屬性的元素.
[!attribute]
匹配不具有指定屬性的元素。
[attribute=value]
匹配具有指定屬性值的元素
[attribute!=value]
匹配不具有指定屬性值的元素
[attribute^=value]
匹配具有指定屬性值開始的元素
[attribute$=value]
匹配具有指定屬性值結束的元素
[attribute*=value]
匹配具有指定屬性的元素,且該屬性包含了一定的值
同時查找多個不同元素 ? $sdom->find(‘a,img’)
層級查找 $sdom->find(‘ul li’); $sdom->find(div a[class=item]);
提示:如果find的第二個參數不指定,返回的是一個鍵值從0開始的對象數組
(2) 用getElementby XXX 查找元素
function getElementById($id) {return $this->find(“#$id”, 0);}
function getElementsById($id, $idx=null) {return $this->find(“#$id”, $idx);}
function getElementByTagName($name) {return $this->find($name, 0);}
function getElementsByTagName($name, $idx=null) {return $this->find($name, $idx);}
還可以通過
function parentNode() {return $this->parent();}
function childNodes($idx=-1) {return $this->children($idx);}
function firstChild() {return $this->first_child();}
function lastChild() {return $this->last_child();}
function nextSibling() {return $this->next_sibling();}
function previousSibling() {return $this->prev_sibling();}
來查找它的父元素 子元素 兄弟元素
資源下載:https://github.com/samacs/simple_html_dom
最后編輯:2014-09-17作者:開心樂窩
總結
以上是生活随笔為你收集整理的simple html dom img,simple_html_dom学习过程(1)查找元素的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html5 css3 设计模式,html
- 下一篇: 21天学通HTML5和CSS3第二版,2