Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.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 使用DOMdocument()方法按类名获取元素_Php_Curl_Domdocument - Fatal编程技术网

Php 使用DOMdocument()方法按类名获取元素

Php 使用DOMdocument()方法按类名获取元素,php,curl,domdocument,Php,Curl,Domdocument,以下是我试图实现的目标:检索页面上的所有产品并将它们放入数组中。以下是我正在使用的代码: $page2 = curl_exec($ch); $doc = new DOMDocument(); @$doc->loadHTML($page2); $nodes = $doc->getElementsByTagName('title'); $noders = $doc->getElementsByClassName('productImage'); $title = $nodes-&g

以下是我试图实现的目标:检索页面上的所有产品并将它们放入数组中。以下是我正在使用的代码:

$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;
它适用于
$标题,但不适用于产品。有关信息,在HTML代码中,img标记如下所示:

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

我一直在看这个()但是我仍然不知道如何使它工作

PS:我得到这个错误:

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


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

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

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

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;
}
函数getElementsByClassName($dom、$ClassName、$tagName=null){ 如果($标记名){ $Elements=$dom->getElementsByTagName($tagName); }否则{ $Elements=$dom->getElementsByTagName(“*”); } $Matched=array(); 对于($i=0;$i长度;$i++){ 如果($Elements->item($i)->attributes->getNamedItem('class')){ 如果($Elements->item($i)->attributes->getNamedItem('class')->nodeValue==$ClassName){ $Matched[]=$Elements->item($i); } } } 返回$Matched; } //用法 $dom=new\DOMDocument('1.0'); @$dom->loadHTML($html); $elementsByClass=getElementsByClassName($dom,$className,'h1');
这个问题很奇怪,因为OP构成了它自己的not exist方法。在重复的问题中给出了一个更正确的xpath变量作为答案:小心,该函数中的stripos检入可能会导致误报。。如果您有一个像FormRowHeader这样的类,那么对于FormRow它仍然会返回true。
$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');
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');