0

当我在 PHP 代码中使用 xPath 查询 HTML 文档时,我尝试从 DOMNodeList 中读取以下 HTML。这是我的查询$description = $xpath->query("//div[@class='qsc-html-content']"); ,我希望我能够获取第二个 div 中的所有内容。我是 PHP 中 xPath 的新手。问题是当我尝试以下任何事情时:

        if(isset($description) && is_object($description)){
        echo "DESCIPTION SET";
        echo $description->tagName;

        //echo $description->getElementsByTagName("p");
        $productInfo['description'] = trim($description->nodeValue);            
    }

我没有得到任何结果。

<div class="product-description ">
<div class="qsc-html-content">
    <p><span class="Apple-style-span" style="background-color: #ffffff; font-family: Verdana, sans-serif;">Farida is a new and upcoming brand in the hookah market which specializes in solid brass hookahs. The designs are very unique and have not been seen in the hookah industry before. </span>This hookah stand about&nbsp;36".</p>
    <ul>
    <li>Farida&nbsp;hose</li>
    <li>Tongs, Grommets, Bowl, &amp; Farida Gold Tray</li>
    </ul>
    <p><span style="color: #ff0000;"><strong><span style="font-size: x-small;">Please Not: </span></strong></span></p>
    <p>&nbsp;Glass bases are mouth blown and sometimes have air bubbles as evidence of this fact. <span style="font-size: xx-small;">Artisans hand paint each glass base, making every one as unique as the artist. This results in a 100% unique finished product that may not look identical to the photo. It also means that sometimes the paint may have a slight smudge, a line may not be perfectly straight, or you may see the artist's brush strokes.</span></p>
    <p>Egyptian products are handmade in a traditional manner. Because these hookahs are handmade, there are usually slight variations from hookah to hookah. Most hookahs contain visible weld lines or unpolished metal at the welds.</p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #ff0000;">FREE STARTER KIT INCLUDES:</span> 1 Holland Charcoal, 10 Mouth Tips, and 2 AL Fakher 50g Tobbacos Select Flavors</p>
    <p><img alt="" height="55" src="media/round tablets.jpg" width="103" />&nbsp;&nbsp;&nbsp;<img alt="" height="81" src="media/male_mouth_tips.jpg" width="109" /> <img alt="" height="86" src="media/d_al_fakher_50g.jpg" width="126" />&nbsp;&nbsp;&nbsp;&nbsp;</p>
</div>

有人可以请指导我哪里错了。还有另一个问题,我试图通过 xPath 查询获取锚标记列表,结果只返回单个锚。

在过去的两个小时里,我一直在努力解决这个问题。我现在筋疲力尽。

谢谢

4

1 回答 1

0

结果$xpath->query(...)是一个 DOMNodeList 对象。它不是单个元素,因此使用:

echo $description->tagName;

是错的。相反,您应该迭代结果,如下所示:

$description = $xpath->query("//div[@class='qsc-html-content']");
foreach ($description as $item) {
    echo $item->tagName . "\n";
}
于 2012-04-25T18:37:19.263 回答