0

我正在尝试从在线商店获取价格。

我在这里使用此代码..

<?php
function getPrice($site){
    $html = file_get_contents($site);
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    $contents = $dom->document.getElementsByTagName("span");        

    $price = "";
    for($i = 0; $i < $contents->length; $i++){
        $content= $contents->item($i);
        if($content->getAttribute('class') == "fk-font-verybig pprice vmiddle fk-bold"){
            $price = $content->getAttribute('value');
        }
    }
    echo $price;
}

$website = "http://www.flipkart.com/sogo-ss-5365-750-w-pop-up-toaster/p/itmdz3hgfjzgfp4v?pid=PUTDYWT2UHPCDCG8&offer=DOTDOnPopUpToaster_Sep2.&icmpid=hp_dotd_3_DOTDOnPopUpToaster_Sep2.";

getPrice($website);


?>

我的脚本返回错误

警告:DOMDocument::loadHTML(): Unexpected end tag : span in Entity, line: 261 in E:\Local server\htdocs\store\scripts\getprice.php on line 5

Warning: DOMDocument::loadHTML(): htmlParseEntityRef: no name in Entity, line: 293 in E:\Local server\htdocs\store\scripts\getprice.php on line 5

...................................................................

Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity, line: 6160 in E:\Local server\htdocs\store\scripts\getprice.php on line 5

Notice: Undefined property: DOMDocument::$document in E:\Local server\htdocs\store\scripts\getprice.php on line 6

Fatal error: Call to undefined function getElementsByTagName() in E:\Local server\htdocs\store\scripts\getprice.php on line 6

是否可以这样获取价格,因为商店不断更改其产品的价格。还有其他替代方法吗?

这个脚本是否会影响我的服务器性能,因为每次用户访问我网站上的产品页面时,它都会从 5 个不同的商店获取价格以比较价格。

4

1 回答 1

0
$contents = $dom->document.getElementsByTagName("span");

您的 $dom->document 失败,因为 DOMDocument 类没有名为“document”的属性。

Notice: Undefined property: DOMDocument::$document in E:\Local server\htdocs\store\scripts\getprice.php on line 6

所以这可能有效

$contents = $dom->getElementsByTagName("span");

以上应该工作。

我建议迭代 $contents 而不是 echo。

甚至 print_r 也可以帮助您查看 $contents 中的节点结构。

于 2014-09-02T12:06:29.160 回答