正如我在问题标题中提到的那样,我正在尝试下面的代码以达到 xpath 结果中所需的节点。
<?php
$xpath = '//*[@id="topsection"]/div[3]/div[2]/div[1]/div/div[1]';
$html = new DOMDocument();
@$html->loadHTMLFile('http://www.flipkart.com/samsung-galaxy-ace-s5830/p/itmdfndpgz4nbuft');
$xml = simplexml_import_dom($html);
if (!$xml) {
echo 'Error while parsing the document';
exit;
}
$source = $xml->xpath($xpath);
echo "<pre>";
print_r($source);
?>
这是源代码。我正在使用从电子商务中取消价格。它工作它给出以下输出:
Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[class] => line
)
[div] => SimpleXMLElement Object
(
[@attributes] => Array
(
[class] => prices
[itemprop] => offers
[itemscope] =>
[itemtype] => http://schema.org/Offer
)
[span] => Rs. 10300
[div] => (Prices inclusive of taxes)
[meta] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[itemprop] => price
[content] => Rs. 10300
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[itemprop] => priceCurrency
[content] => INR
)
)
)
)
)
)
现在如何直接到达 [内容] => 卢比。10300. 我试过了:
echo $source[0]['div']['meta']['@attributes']['content']
但它不起作用。