Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP&;DOM:如何使用类名搜索单个元素?_Php_Html_Xpath_Domdocument - Fatal编程技术网

PHP&;DOM:如何使用类名搜索单个元素?

PHP&;DOM:如何使用类名搜索单个元素?,php,html,xpath,domdocument,Php,Html,Xpath,Domdocument,我试图搜索一系列HTML元素并提取某些div中的文本(基于类名),但是我似乎无法搜索单个元素,只能搜索所有节点 <html> <div class=parent> <div videoid=1></div> <div class=inner>Testing <div class=title>Test</div> <div class=date>Test&

我试图搜索一系列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;
}

测试
试验
试验
试验
测试
试验
试验
试验
测试
试验
试验
试验
$url=新文档;
$url->loadHTMLFile(“text.html”);
$finder=newdomxpath($url);
$classname=“父级”;
$nodes=$finder->query(“/*[contains(concat(“”,规范化空间(@class),“”),“$classname”)”)”;
$count=0;
foreach($nodes as$element){//将父div的每个实例提取到它自己的元素中。
//在父div中,提取以下子div中属于以下属性的videoid属性的值:videoid;
//在父div中,提取以下子div中属于以下类的文本:title;
//在父div中,提取以下子div中属于以下类的文本:date;
//在父div中提取以下子div中属于以下类的文本:time;
}

虽然每个父元素中的每个子元素只有一个实例,但它们在父div中的顺序可以是任意的,并且可以是它们自己的子元素。本质上,我在寻找某种递归搜索,我认为?

从您得到的
父元素中,您可以继续搜索您需要的值<代码>->query(表达式,上下文节点)
有第二个参数,您可以将上下文节点放在需要搜索的位置

粗略的例子:

// 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/>';
    }
}
$finder=newdomxpath($url);
$classname=“父级”;
$parents=$finder->query(//div[@class='$classname']);
如果($parents->length>0){
foreach($parents作为$parent){
$id=$finder->query('./div[@class=“id”]”,$parent)->项(0)->节点值;
$title=$id=$finder->query('./div[@class=“internal”]/div[@class=“title”]”,$parent)->项(0)->节点值;
$date=$id=$finder->query('./div[@class=“internal”]/div[@class=“date”]”,$parent)->项(0)->节点值;
$time=$id=$finder->query('./div[@class=“internal”]/div[@class=“time”]',$parent)->item(0)->nodeValue;
回显$id,
,$title,
,$date,
,$time,
; } }

当你期望结构总是这样时,情况就是这样。如果标记灵活,您可以使用查询在父级内部搜索,并获得找到的第一个:

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

您不能只搜索
div[@class=“parent”]
,您的标记似乎与之不匹配。只需使用上下文节点来获取其他子节点,我就是这么做的?这使我将每个父节点作为它自己的元素(在for-each循环中),但它不允许我以相同的方式搜索这些元素。我又在用错误的方式寻找吗?我不应该使用$finder->query吗?是的@John你可以在这个循环(找到的父元素)下搜索剩余值(chilren)。这就是我现在在for循环中所做的:foreach($nodes as$element){$finder=new DomXPath($element);$classname=“date”;$dates=$finder->query(//*[包含(concat('',规范化空间(@class)但是我得到的错误是:“可捕获的致命错误:传递给DOMXPath的参数1::_construct()必须是DOMDocument的实例,domeElement的实例给定”我注意到您使用了项(0),现在子元素可能在任何顺序内,并且在任何数量的内部div内,不仅仅是在父母的直接领导下。我怎么解释呢?我想我需要一个递归搜索,搜索所有父级的子级和他们自己的子级,直到找到与该类匹配的元素为止?@JohnBergqvist yoiu需要相应地更改查询,在edithmmm上,它现在根本没有返回任何内容:/我有没有办法只查看每个子元素&每个父元素的子元素?万一我把结构搞错了什么的?@JohnBergqvist你要测试的html在哪里?如果你能看到的话,我只是在演示中的每个父级上模拟了一个混乱的元素示例。您的意思是迭代每个
父项中的所有元素吗?是的。代码有这么多无关的元素,都是以随机顺序排列的,所以可能太复杂了,无法解析?关键是它需要能够搜索每个元素,包括它的子元素。例如,ID div可以是父类中另一个类中的几个div。