我刚开始在PHP中使用Xpath作为项目。我必须从网站上获得每种产品的单独价格。每个产品的价格信息都具有以下格式:
<div class="priceStockDetail">
<dl>
<dt>Across any 6</dt>
<dd><span class='price '><span class='currency'>$</span>20<span class=cents>.90</span></span></dd>
<dt>Each</dt>
<dd><span class='price '><span class='currency'>$</span>22<span class=cents>.00</span></span></dd>
</dl>
</div>
我希望只将<dt>Each</dt>
之后的文本作为一个(上面例子为“22.00美元”),并带有Xpath表达式。
非常感谢任何帮助。
答案 0 :(得分:0)
您既可以定位每个棋子元素并连接,也可以只定位父元素并获取nodeValue。例如:
$dom = new DOMDocument();
$dom->loadHTML($html_string);
$xpath = new DOMXpath($dom);
$data = array();
$products = $xpath->query('//dt');
if($products->length > 0) {
foreach($products as $product) {
$product_name = $product->nodeValue;
// either get individually
// $currency = $xpath->evaluate('string(./following-sibling::dd[1]/span/span[@class="currency"])', $product);
// $price = $xpath->evaluate('string(./following-sibling::dd[1]/span/span[@class="currency"]/following-sibling::text())', $product);
// $cents = $xpath->evaluate('string(./following-sibling::dd[1]/span/span[@class="cents"])', $product);
// $product_price = $currency.$price.$cents;
// or
$product_price = $xpath->evaluate('string(./following-sibling::dd[1]/span)', $product);
$data[] = array(
'product_name' => $product_name,
'product_price' => $product_price,
);
}
}
echo '<pre>';
print_r($data);