2011-09-18 48 views
2
<?php 

    # don't forget the library 
    include('simple_html_dom.php'); 

    # this is the global array we fill with article information 
    $Prices = array(); 

    getPrices('http://www.google.com/search?q=xbox+360&tbm=shop&hl=en&aq=f'); 

function getPrices($page) { 
    global $Prices, $descriptions; 

    $html = new simple_html_dom(); 
    $html->load_file($page); 

    $items = $html->find('div.psliprice'); 

    foreach($items as $post) { 
     # remember comments count as nodes 
     $Prices[] = $post->children(0)->outertext; 
    } 
} 

?> 


<html> 
<head> 
    <style> 
     #main { 
      margin: 80px auto; 
      width: 600px; 
     } 
     h1 { 
      font: bold20px/30px verdana, sans-serif; 
      text-decoration: none; 
     } 
     p { 
      font: 10px/14px verdana, sans-serif; 
    </style> 
</head> 
<body> 
    <div id="main"> 
<?php 
    foreach($Prices as $item) { 
     echo $item[0]; 
     #echo $item[1]; 
    } 
?> 
    </div> 
</body> 
</html> 

以上只是輸出:<<<<<<<<<<任何人都知道這是爲什麼發生?從頁面刮價格分類Php

回答

1

你有一個語法錯誤:

$items = $html->find('div[class=psliprice]"'); 

試試這個:

$items = $html->find('div[class="psliprice"]'); 

而且(我可能是錯的),沒有谷歌有這樣的請求的API?


試試這個代碼,而不是:

$Prices[] = $post->children(0)->outertext; 

,並刪除了echo $item[1];線。

+0

查看更新的代碼和相關的錯誤請 – m67

+0

查看我的更新。 DOM似乎更簡單一些。 – Blender

+0

做這兩個編輯不會做任何事情。請參閱更新的代碼。 – m67