php 使用 DOMdocument() 方法通过 ClassName 获取元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20728839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:28:29  来源:igfitidea点击:

Get Element by ClassName with DOMdocument() Method

phpcurldomdocument

提问by justberare

Here is what I am trying to achieve : retrieve all products on a page and put them into an array. Here is the code I am using :

这是我想要实现的目标:检索页面上的所有产品并将它们放入一个数组中。这是我正在使用的代码:

$page2 = curl_exec($ch);
$doc = new DOMDocument();
@$doc->loadHTML($page2);
$nodes = $doc->getElementsByTagName('title');
$noders = $doc->getElementsByClassName('productImage');
$title = $nodes->item(0)->nodeValue;
$product = $noders->item(0)->imageObject.src;

It works for the $title butnot for the product. For info, in the HTML code the img tag looks like this :

它适用于$title but非产品。有关信息,在 HTML 代码中,img 标签如下所示:

<img alt="" class="productImage" data-altimages="" src="xxxx">

I have been looking at this (PHP DOMDocument how to get element?) but I still don't understand how to make it work.

我一直在看这个(PHP DOMDocument 如何获取元素?)但我仍然不明白如何使它工作。

PS : I get this error :

PS:我收到此错误:

Call to undefined method DOMDocument::getElementsByclassName()

调用未定义的方法 DOMDocument::getElementsByclassName()

回答by justberare

I finally used the following solution :

我最终使用了以下解决方案:

    $classname="blockProduct";
    $finder = new DomXPath($doc);
    $spaner = $finder->query("//*[contains(@class, '$classname')]");

回答by Ulad Kasach

https://stackoverflow.com/a/31616848/3068233

https://stackoverflow.com/a/31616848/3068233

Linking this answer as it helped me the most with this problem.

链接这个答案,因为它对我解决这个问题的帮助最大。

function getElementsByClass(&$parentNode, $tagName, $className) {
    $nodes=array();

    $childNodeList = $parentNode->getElementsByTagName($tagName);
    for ($i = 0; $i < $childNodeList->length; $i++) {
        $temp = $childNodeList->item($i);
        if (stripos($temp->getAttribute('class'), $className) !== false) {
            $nodes[]=$temp;
        }
    }

    return $nodes;
}

Theres the code and heres the usage

代码如下,用法如下

$dom = new DOMDocument('1.0', 'utf-8');
$dom->loadHTML($html);
$content_node=$dom->getElementById("content_node");

$div_a_class_nodes=getElementsByClass($content_node, 'div', 'a');

回答by Mahbub Alam

function getElementsByClassName($dom, $ClassName, $tagName=null) {
    if($tagName){
        $Elements = $dom->getElementsByTagName($tagName);
    }else {
        $Elements = $dom->getElementsByTagName("*");
    }
    $Matched = array();
    for($i=0;$i<$Elements->length;$i++) {
        if($Elements->item($i)->attributes->getNamedItem('class')){
            if($Elements->item($i)->attributes->getNamedItem('class')->nodeValue == $ClassName) {
                $Matched[]=$Elements->item($i);
            }
        }
    }
    return $Matched;
}

// usage

    $dom = new \DOMDocument('1.0'); 
    @$dom->loadHTML($html);
    $elementsByClass = getElementsByClassName($dom, $className, 'h1');