如何只用xpath得到div的内容?

时间:2017-04-10 19:29:00

标签: php parsing xpath

我需要解析许多页面并仅在div上获取内容" description"类。

$curl = curl_init('http://optnow.ru/products/nabor:-setevoj-i-avtomobilnyj-adapter-pitaniya-dlya-noutbukov-deppa-slim-universalnyj-90-vt-d-21104-12-konnektorov-chernyj');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10');
$catalogHtml = curl_exec($curl);
curl_close($curl);
$domCatalog = new DomDocument();
@$domCatalog->loadHTML( $catalogHtml );
$catalogData = new DomXPath( $domCatalog );
$xpathCatalogResult = $catalogData->query("//div[@class='tab-content description']//div//text()");
print_r($xpathCatalogResult);

但如果我使用它,我只能

DOMNodeList Object
(                                                                                                                                                                                                                    
    [length] => 1
)

我如何获得此内容?

2 个答案:

答案 0 :(得分:0)

谢谢大家,我明白了。

解决方案:

    foreach ($xpathCatalogResult as $key) {
        print_r($key->nodeValue);
    }

答案 1 :(得分:0)

Xpath表达式可以返回标量值。但只有DOMXpath:evaluate()支持。

var_dump( 
  $catalogData->evaluate("string(//div[@class='tab-content description']//div)")
);