2014-09-02 47 views
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); 


?> 

我的腳本返回錯誤

警告:DOM文檔:: loadHTML():預期的結束標記:跨度實體,行:261 E: \本地服務器\ htdocs中\店\腳本\ getprice.php第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個不同的商店獲取價格,以便共同價格。

+1

如果您有正式的法定許可從他們的網站獲取價格,您應該聯繫他們獲取xml或csv格式的價格。是的,它會影響頁面加載。你應該緩存價格(memcache,redis,sql)。 – DanFromGermany 2014-09-02 11:49:43

+0

你可以只刪除點,並嘗試一次DOTDOnPopUpToaster_Sep2。&icmpid = hp_dotd_3_DOTDOnPopUpToaster_Sep2。? – 2014-09-02 11:51:08

+0

想要在MySQL中存儲價格以減少從再次獲取表單服務器的時間 – user3811305 2014-09-02 11:51:49

回答

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

您的$ dom->文檔失敗,因爲DOMDocument類沒有名爲'document'的屬性。

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

所以這可能工作

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

以上應該工作。

我建議迭代$內容而不是echo。

即使print_r也可以幫助您查看$ contents中的節點結構。

+0

thnx幫助,但現在我只是收到警告,但沒有輸出 – user3811305 2014-09-02 12:16:45

+0

什麼是警告? – MontrealDevOne 2014-09-02 12:29:24

+0

警告:DOMDocument :: loadHTML():意外的結束標記:實體中的跨度,行E:\ Local服務器上的261行:\ htdocs \ store \ scripts \ getprice.php上的第6行 警告:DOMDocument :: loadHTML() :htmlParseEntityRef:實體中沒有名稱,行:293行E:\ Local server \ htdocs \ store \ scripts \ getprice.php第6行 警告:DOMDocument :: loadHTML():htmlParseEntityRef:實體中沒有名稱,行: 293行E:\本地服務器\ htdocs \ store \ scripts \ getprice.php在第6行 .... 警告:DOMDocument :: loadHTML():htmlParseEntityRef:expect';'在第6行的E:\ Local server \ htdocs \ store \ scripts \ getprice.php中的實體行中:6125 – user3811305 2014-09-02 12:37:02