我在PHP代码中使用xPath查询HTML文档时,我正在尝试从DOMNodeList中读取HTML。这是我的查询$description = $xpath->query("//div[@class='qsc-html-content']");
我希望我能够获取第二个div内的所有东西。我是PHP的新手。问题是当我尝试以下任何事情时:
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 36".</p>
<ul>
<li>Farida hose</li>
<li>Tongs, Grommets, Bowl, & Farida Gold Tray</li>
</ul>
<p><span style="color: #ff0000;"><strong><span style="font-size: x-small;">Please Not: </span></strong></span></p>
<p> 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> <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" /> <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" /> </p>
</div>
有人可以指导我在哪里错了。还有另一个问题,我试图通过xPath查询获取锚标记列表,结果只返回单个锚点。
我最近2个小时撞到了这头。我现在很累。
感谢
答案 0 :(得分:0)
$xpath->query(...)
的结果是DOMNodeList对象。它不是单个元素,所以使用:
echo $description->tagName;
错了。相反,你应该迭代结果,如下所示:
$description = $xpath->query("//div[@class='qsc-html-content']");
foreach ($description as $item) {
echo $item->tagName . "\n";
}