PHP简单HTML DOM - 方法查找检索空数组

时间:2017-11-09 21:54:46

标签: php dom php-parser

现在我正在尝试编写一个PhP解析器,但我不知道为什么我的代码返回一个空数组。我正在使用PHP Simple HTML DOM。我知道我的代码并不完美,但它仅用于测试。

我将不胜感激任何帮助

    public function getData() {
    // get url form urls.txt
    foreach ($this->list_url as $i => $url) {
        // create a DOM object from a HTML file
        $this->html = file_get_html($url);
        // find array all elements with class="name" because every products having name
        $products = $this->html->find(".name");

        foreach ($products as $number => $product) {
            // get value attr a=href product
            $href = $this->html->find("div.name a", $number)->attr['href'];
            // create a DOM  object form a HTML file
            $html = file_get_html($href);

            if($html && is_object($html) && isset($html->nodes)){
                echo "TRUE - all goodly";
            } else{
                echo "FALSE - all badly";
            }
            // get all elements class="description"
            // $nodes is empty WHY? Tough web-page having content and div.description?
            $nodes = $html->find('.description');
            if (count($nodes) > 0) {
                $needle = "Производитель:";
                foreach ($nodes as $short_description) {
                    if (stripos($short_description->plaintext, $needle) !== FALSE) {
                        echo "TRUE";
                        $this->data[] = $short_description->plaintext;
                    } else {
                        echo "FALSE";
                    }
                }
            } else {
                $this->data[] = '';
            }

            $html->clear();
            unset($html);
        }

        $this->html->clear();
        unset($html);
    }
    return $this->data;
}

0 个答案:

没有答案