2015-05-12 38 views
1

我想通過一系列HTML元素進行搜索並提取某些div(基於類名稱)中的文本,但是我似乎無法搜索單個元素,只搜索所有節點。PHP&DOM:如何使用類名稱搜索單個元素?

<html> 
<div class=parent> 
    <div videoid=1></div> 
    <div class=inner>Testing 
     <div class=title>Test</div> 
     <div class=date>Test</div> 
     <div class=time>Test</div> 
    </div> 
</div> 

<div class=parent> 
    <div videoid=2></div> 
    <div class=inner>Testing 
     <div class=title>Test</div> 
     <div class=date>Test</div> 
     <div class=time>Test</div> 
    </div> 
</div> 

<div class=parent> 
    <div videoid=3></div> 
    <div class=inner>Testing 
     <div class=title>Test</div> 
     <div class=date>Test</div> 
     <div class=time>Test</div> 
    </div> 
</div> 
</html> 
$url = new DOMDocument; 
$url->loadHTMLFile("text.html"); 

$finder = new DomXPath($url); 
$classname="parent"; 
$nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]"); 
$count = 0; 
foreach($nodes as $element) { //extracts each instance of the parent div into it's own element. 

//within the parent div extract the value for the videoid attribute within the following child div belonging to the following attribute: videoid; 

//within the parent div extract the text within the following child div belonging to the following class: title; 

//within the parent div extract the text within the following child div belonging to the following class: date; 

//within the parent div extract the text within the following child div belonging to the following class: time; 
} 

雖然只有一個每個每個父母中的子元素的情況下,他們可能是在父DIV任何順序,並能與自己的孩子。基本上我正在尋找某種遞歸搜索,我認爲?

+0

你就不能搜索'的div [@類=「父「]',你的標記似乎不符合。只需使用上下文節點獲取 – Ghost

+0

下的其他子節點,這就是我所做的?這讓我每個父節點,因爲它是自己的元素(在每個循環內),但它不會讓我以相同的方式搜索這些元素。我再次以錯誤的方式尋找?我應該不使用$ finder->查詢嗎? –

+0

是@John你可以搜索該循環下的重新生成的值(找到的父元素) – Ghost

回答

0

從你得到的parent(元素),你可以繼續搜索你需要的值。 ->query(expression, context node)有第二個參數,您可以從需要搜索的位置放置上下文節點。

粗糙例如:

// for each found parent node 
foreach($parents as $parent) { 
    $id = $finder->query('./div[@class="id"]', $parent)->item(0)->nodeValue; 
    // create another query     ^using the found parent as your context node 
} 

所以在應用這些:

$finder = new DomXPath($url); 
$classname = "parent"; 
$parents = $finder->query("//div[@class='$classname']"); 
if($parents->length > 0) { 
    foreach($parents as $parent) { 
     $id = $finder->query('./div[@class="id"]', $parent)->item(0)->nodeValue; 
     $title = $id = $finder->query('./div[@class="inner"]/div[@class="title"]', $parent)->item(0)->nodeValue; 
     $date = $id = $finder->query('./div[@class="inner"]/div[@class="date"]', $parent)->item(0)->nodeValue; 
     $time = $id = $finder->query('./div[@class="inner"]/div[@class="time"]', $parent)->item(0)->nodeValue; 

     echo $id, '<br/>', $title, '<br/>', $date, '<br/>', $time, '<hr/>'; 
    } 
} 

Sample Output

這就是當你想到結構是這樣的總是如此。您可以在父裏面只是搜索與查詢,並獲得第一個發現,如果標記將是靈活的:

foreach($parents as $parent) { 
    $title = $finder->evaluate('string(.//*[@class="title"][1])', $parent); 
    echo $title, '<br/>'; 
} 

Sample Output

+0

我注意到你已經使用了item(0),現在很可能兒童元素將處於任何順序之內,並且在任何數量的內部div中,不僅僅是直接在父母之下。我將如何解釋這一點?我想我需要一個遞歸搜索來搜索所有父母的孩子和他們自己的孩子,直到找到與該類相匹配的元素爲止。 –

+0

@JohnBergqvist yoiu'll需要相應地更改查詢,編輯 – Ghost

+0

嗯,它現在不會返回任何東西:/有什麼方法可以讓我查看每個父元素的每個孩子及其子元素嗎? incase我有結構錯誤或什麼的? –