我想通過一系列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任何順序,並能與自己的孩子。基本上我正在尋找某種遞歸搜索,我認爲?
你就不能搜索'的div [@類=「父「]',你的標記似乎不符合。只需使用上下文節點獲取 – Ghost
下的其他子節點,這就是我所做的?這讓我每個父節點,因爲它是自己的元素(在每個循環內),但它不會讓我以相同的方式搜索這些元素。我再次以錯誤的方式尋找?我應該不使用$ finder->查詢嗎? –
是@John你可以搜索該循環下的重新生成的值(找到的父元素) – Ghost