【问题标题】:How to reach till the desired node in xpath result?如何到达 xpath 结果中的所需节点?
【发布时间】:2012-12-12 15:17:45
【问题描述】:

正如我在问题标题中提到的,我正在尝试下面的代码以到达 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']

但它不起作用。

【问题讨论】:

标签: php xml xpath web-scraping simplexml


【解决方案1】:

试试echo (String) $source[0]-&gt;div-&gt;meta[0]['content'];

基本上,当你看到一个元素是一个对象时,你不能像数组一样访问它,你需要使用对象-&gt;的方法。

【讨论】:

  • 我试图回显 echo $source[0]->div->meta[0]->getAttribute('content');但它给出错误!还是我误解了这些东西?
  • SCREAM: 忽略 (!) 的错误抑制致命错误:在第 14 行的 C:\wamp\www\pom\fk.php 中调用未定义的方法 SimpleXMLElement::getAttribute()
  • 哦,对了,这是我的错。最近使用 DOM 的工作太多了。试试echo (String) $source[0]-&gt;div-&gt;meta[0]['content'];
【解决方案2】:

SimpleXMLElementprint_r 不显示真实 对象结构。所以你需要有一些知识:

$source[0]->div->meta['content']
        |    |     |      `- attribute acccess
        |    |     `- element access, defaults to the first one
        |    `- element access, defaults to the first one
        |
 standard array access to get 
 the first SimpleXMLElement of xpath()
 operation

然后该示例(带有您的地址)如下(再次为print_rDemo):

SimpleXMLElement Object
(
    [0] => Rs. 10300
)

如果您需要文本值,请将其转换为字符串:

$rs = (string) $source[0]->div->meta['content'];

但是,您已经可以使用 xpath 表达式直接访问该节点(如果这是单一情况)。

详细了解如何在Basic SimpleXML usage ExamplesDocs 中访问SimpleXMLElement

【讨论】:

  • 在这种情况下要更好地替换print_r,请查看github.com/IMSoP/simplexml_debug
  • @IMSoP:哦,太好了。但是请不要将软件置于知识共享许可下,这不适合软件。 编辑: 我需要将此链接到:stackoverflow.com/a/8631974/367456
  • @hakre 这只是署名,没有 copyleft,所以想法是只保留该文档块就足够了。不过我不是专家,所以不确定这是否合法。
  • 知识共享许可的问题在于它们不是为软件设计的。正如许多开发人员所知道的那样,他们在其下的代码存在问题。如果您想要署名(“仅”),我会说选择MITApache 2.0 许可证(取决于您喜欢的详细程度) - 这两个许可证是众所周知的,被接受的,需要保留版权标题并且是许可(无版权)。如果你问我意见,你可能只想要MIT。 ;)
  • @hakre 我知道 CC 许可证通常可能会出现问题,但 CC-BY 几乎是最低限度的。与您链接的两个不同,该 URL 被明确允许包含术语,而不是在某处粘贴完整的样板。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 2021-12-31
  • 2012-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多