【问题标题】:PHP Simple DOM find not workingPHP Simple DOM 发现不工作
【发布时间】:2013-05-02 23:32:40
【问题描述】:

这让我停留了一段时间,所以我想我会发布它。我的问题是我的 find-> 工作不正常并且 $product_name 出现空

    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;

    }

【问题讨论】:

    标签: php dom screen-scraping simpledom


    【解决方案1】:

    尽管我为其他人发布此内容,但我仍然不确定为什么会出现以下情况,如果有人可以向我指出,我会很高兴。

    我发现问题是:

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

    应该是

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

    基本上产品名称必须编号。但是我仍然感到困惑,因为这里: @ 987654324@ find 命令对我有用 find 不需要编号,而且我认为选择器也是一个 ID 的事实将使它变得不必要,如果有人能指出我错过了什么会很棒。

    【讨论】:

    • find 方法返回一个对象数组。如果您指定第二个参数(在您的情况下为0),则它会从数组中返回该键处的对象。它在文档simplehtmldom.sourceforge.net/manual_api.htminnertext 中不是为数组设置的,而是为单个对象设置的。
    猜你喜欢
    • 2012-06-08
    • 2017-03-01
    • 2012-07-21
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    相关资源
    最近更新 更多