gpt4 book ai didi

PHP 和 DOM : How can I search through a single element using class names?

转载 作者:行者123 更新时间:2023-11-28 01:31:14 24 4
gpt4 key购买 nike

我正在尝试搜索一系列 HTML 元素并提取某些 div 中的文本(基于类名),但是我似乎无法搜索单个元素,只能搜索所有节点。

<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 中的顺序可以是任意的,并且可以与它们自己的子元素在一起。本质上我在寻找某种我认为的递归搜索?

最佳答案

从您获得的 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

关于PHP 和 DOM : How can I search through a single element using class names?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30185767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com