2013-05-02 65 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