日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

simple html dom img,simple_html_dom学习过程(1)查找元素

發布時間:2025/3/19 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)查找元素的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。