0

I've started writing a scraper for one site that will also have a crawler, since I need to go through some links, but I'm getting this error :

PHP Fatal error: Uncaught Error: Call to a member function find() on null in D:\Projekti\hemrank\simple_html_dom.php:1129 Stack trace:

0 D:\Projekti\hemrank\scrapeit.php(37): simple_html_dom->find('ul')

1 D:\Projekti\hemrank\scrapeit.php(19): ScrapeIt->getAllAddresses()

2 D:\Projekti\hemrank\scrapeit.php(55): ScrapeIt->run()

3 {main} thrown in D:\Projekti\hemrank\simple_html_dom.php on line 1129

When I var_dump the $html variable I get the full html with all the tags, etc, that's why it's strange to me that it says "Call to a member function find() on null", when there's actually value in the $html. Here's the part of the code that's not working :

        $html = new simple_html_dom();
        $html->load_file($baseurl);
        if(empty($html)){echo "HTTP Response not received!<br/>\n";exit;}
        $links = array();
        foreach ($html->find('ul') as $ul) {
            if(!empty($ul) && (count($ul)>0))
            foreach ($ul->find('li') as $li) {
                if(!empty($li) && (count($li)>0))
                foreach ($li->find('a') as $a) {
                    $links[] = $a->href;
                }
                else
                    die("NOT AVAILABLE");
            }
        }
        
        return $links;

    }

Is this a common problem with PHP simple HTML DOM parser, is there a solution or should I switch to some other kind of scraping?

11
  • PHP does not provide a simple_html_dom class. Commented Sep 14, 2017 at 13:03
  • I think It should be just $html = file_get_html($baseurl); Commented Sep 14, 2017 at 13:03
  • See this link Commented Sep 14, 2017 at 13:05
  • @axiac no it doesn't, you download it and include it in the file where you're going to use it.. Commented Sep 14, 2017 at 13:06
  • 1
    @teeyo that's the one I'm asking a question about.... the whole title says PHP simple HTML DOM error Commented Sep 14, 2017 at 13:09

2 Answers 2

0

I just searched for the lib you are using, this is line 1129:

return $this->root->find($selector, $idx, $lowercase);

So your error message is telling you that $this->root inside the class is null, therefore no find() method exists!

I'm no expert on the lib, as I use the awesome DOMDocument for parsing HTML, but hopefully this should help you understand what has happened.

Also, $html will never be empty in that code of yours, you already populated it when you instantiated it!

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

2 Comments

It's possible, and it could be the first $html->find('ul') which means the $html->load_file($baseurl) couldn't load the file!
var_dump($html), to see what root is actually set at
0

I suggest the following change:

$html->load_file($baseurl); to $html = file_get_html($baseurl);

On my VPS server it works with $html->load_file($baseurl); but on my dedicated local server it only works with $html = file_get_html($baseurl);

This solved my problem: - Call to a member function find() on null - simple_html_dom.php on line 1129

Comments

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.