我想通过一系列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