2013-05-02 62 views
0

這讓我陷入了一段時間,所以我想我會發布它。我的問題是,我find->不正常工作和$ PRODUCT_NAME就要到了空PHP簡單DOM找不到工作

require 'mysql_con.php'; 
    require 'simple_html_dom.php'; 

    $html = file_get_html('http://www.xxxx.com/index.php?main_page=index&cPath=23'); 

    /* foreach($html->find('img') as $element) { 
      echo $element->src . '<br>'; 
     } */ 

    $find = $html->find('#specialsListing .specialsListBoxContents .indent a'); 

    $i=0; 

    foreach ($find as $test) { 

     $link = html_entity_decode($test->href); 

     $linkgrab = file_get_html($link); 

     $product_name = $linkgrab->find('#productName')->innertext; 

     echo $product_name; 

     break; 

    } 

回答

1

至於因爲我張貼這對於其他人,我仍然不確定爲什麼下面的話,他會喜歡,如果多有人可以向我指出。

我計算出的問題是:

$product_name = $linkgrab->find('#productName')->innertext;

應該

$product_name = $linkgrab->find('#productName', 0)->innertext;

基本上產品名稱有編號。然而,我仍然困惑,因爲在這裏: $find = $html->find('#specialsListing .specialsListBoxContents .indent a');查找命令爲我找到沒有需要編號,也認爲選擇器是一個ID的事實也會使它不必要,如果有人可以指出我是什麼錯過這將是偉大的。

+2

find方法返回一個對象數組。如果你指定第二個參數(在你的情況下爲'0'),那麼它返回數組中該鍵的對象。它在文檔http://simplehtmldom.sourceforge.net/manual_api.htm innertext中沒有爲數組設置,而是針對單個對象。 – 2013-05-02 23:41:07