0

Now I am trying to write a PhP parser and I don't why my code return an empty array. I am using PHP Simple HTML DOM. I know my code is't perfect, but it's only for testing.

I will be appreciate for any help

    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;
}
2
  • In this case - all good if i put "http://................." instead $href // create a DOM object form a HTML file $html = file_get_html("http://................."); Commented Nov 9, 2017 at 22:22
  • Unfortunately I can't solve this problem. If i will put "http://................." instead $href then script will get a data from one page, but i need script getting data from every page! Commented Nov 9, 2017 at 22:33

1 Answer 1

0

hi you should inspect the element and copy->copy selector and use it in find method to getting the object

Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.