Troubleshooting common issues with PHP's simple HTML DOM parser

I've encountered an issue while working on a scraper for a website that involves crawling through links. The error message I'm receiving is as follows:

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

Even after var_dumping the $html variable and confirming it contains the full HTML content, the error "Call to a member function find() on null" persists. Here's the section of code causing trouble:

        $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;

    }

Has anyone else experienced this issue with PHP's simple HTML DOM parser? Are there any known solutions, or should I consider using a different scraping method?

Answer №1

After conducting a search for the library you are utilizing, I found the following line of code on line 1129:

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

It appears that your error message is indicating that $this->root within the class is currently set to null, hence the absence of the find() method!

While I may not be well-versed in this particular library, as I typically rely on the reliable DOMDocument for HTML parsing, I hope this explanation sheds some light on the issue for you.

In addition, it's worth noting that $html will always have a value in the provided code snippet, as it was assigned a value during instantiation.

Answer №2

A recommended modification is as follows:

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

In my experience, $html->load_file($baseurl); functions properly on my VPS server, but only $html = file_get_html($baseurl); works on my dedicated local server.

The solution that worked for me was: - Eliminating the error "Call to a member function find() on null" - Addressing the issue in simple_html_dom.php at line 1129

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Alter the border hue of the checkbox and radio button

Is there a way to modify the border color of checkboxes and radio buttons (both circle and rectangle) using CSS? I've attempted several methods with no success. Can someone provide guidance on this? Any advice would be greatly appreciated. Thank you ...

Universal compatibility for web displays

I've been conducting testing on a website across Chrome, Firefox, and Internet Explorer, utilizing the CSS code snippet below: #foot_links1, #foot_links2, #foot_links3 { position: absolute; margin-top: 55px; margin-top: 14em; color: # ...

Creating nested directories in PHP using arrays: A step-by-step guide

When working with Laravel, I encountered an issue while trying to create a trait file using the console. The problem arises when specifying a directory for the file creation - it does not get created as expected. The structure of the directory can be dyna ...

Exploring the different ways to compare data with the load() method

I am faced with a coding dilemma. I have two pages: data.php and index.html. The data.php page contains a single value (let's say 99), while the index.html page uses jQuery to dynamically load the data from data.php into a div called "data" every 2 se ...

The jQuery's load function appears to be malfunctioning

$toolTip_inner.load('ajax/fetchUser_data.php',{ id: $tooltipText }); I am attempting to load the content of a PHP file into the toolTip_inner element while passing some parameters. The fetchUser_data.php file executes a MySQL statement and retr ...

Issue: The plugin 0 mentioned in the file "/my dir/node_modules/babel-preset-php/src/index.js" contains an invalid property called "default"

While attempting to convert a PHP script to JavaScript using babel-preset-php, I encountered the following error: Error: Plugin 0 specified in "/media/deep/5738c180-2397-451b-b0b5-df09b7ad951e1/deepx/Documents/TestingAll/node_modules/babel-preset-php/ ...

I'm having trouble getting the float left to work properly in my HTML/CSS code

Currently, I am diving into the world of HTML / CSS on my own. After following some tutorials, I have taken the leap to work on my very first project. My goal is to layout three images and three text elements on a single page using CSS. The desired struct ...

Unusual redirection occurring between two subdomains

I'm currently in the process of updating my .htaccess file with the following changes... previous url: http://piesyearly.com/ new url: http://yearly.pies.com/ both of these are located within the directory structure below: /public_html htaccess.tx ...

Assistance for WPML Integration with WooCommerce

My website uses multiple languages (English, French, Serbian) with a WooCommerce setup created using the qTranslate plugin. However, I am facing an issue with translating WooCommerce messages such as cart, checkout, and email contents. I was able to solve ...

Save a randomly generated string in the Session, which will be replaced when accessed through a different PHP file

I have created a form with a hidden input field, and its value is dynamically set using a function. <?php class Token { public static function generate(){ return $_SESSION['token'] = base64_encode(openssl_random_pseudo ...

What is the best way to center the input on the page?

Does anyone know how to align the <input type="text"> in the center? I'm trying to create a login form but having trouble with positioning the input element. ...

The radio buttons are stuck and not changing their selection

Having a group of radio buttons with the same name, when one is checked, it automatically selects another one in the group. Here is my current code: <input name="a" type="radio"> <input name="a "type="radio" checked> JS $("input[type='r ...

Version 1 of Vue.js is not compatible with Ajax-loaded HTML files

Currently, I am encountering a minor issue with loading content through ajax requests. I am in the process of developing a web application where everything is located on one page without any reloading. To achieve this, I have split the content into separa ...

Arranging arrows strategically along a line and ensuring they are positioned correctly above all div elements

I'm really struggling with this issue. I have a situation where I need to center a downward pointing arrow within some divs. It should be positioned in the middle, on a brown line, and on top of everything else. The last div should also include the a ...

Interested in compressing CSS and JavaScript using PHP, but uncertain about the impact on performance and the best methods to implement it?

My current approach involves using PHP to combine multiple css (or js) files into a single file, while also compressing the content using GZIP. For example, the HTML page makes calls to resources like this... <link rel="stylesheet" href="Concat.php?fi ...

Is there a way to ensure uniform font display across all browsers?

Is there a way to ensure consistent font display across all browsers? I am facing an issue with font consistency on my webpage. While browsers like Internet Explorer Edge and 11, Chrome, and Firefox display the desired font correctly, Internet Explorer 8 ...

What is the equivalent in Go of reading from the PHP standard input stream?

Currently I am in the process of adapting a PHP script that is designed to monitor events on a Linux system through supervisor. Forgive me for possibly asking a naive question, but I am struggling to determine what the equivalent of the following code sni ...

What steps can be taken to modify the JSON encoding API format in Laravel?

query: $prop_map = DB::table('jk_property_map_jpm')->select('jpm_location','jpm_longitude','jpm_latitude')->where('jpm_location','like','%'.$par2.'%')->get(); foreac ...

What is the best way to retrieve the content of a file and set it as the value

exam.php <div class='btitle'>LOREM</div> main.php <?php $content = file_get_contents('exam.php'); ?> <textarea class='txa' value = <?php echo $content; ?>></textarea> outcome text ...

Unique mechanism for tracking clicks in ASP and C#

Is there a preferred method for showing a click counter on an HTML page when working with a .aspx file that connects to a .cs file pulling data from a SQL server, without relying on an external text file for tracking purposes? I'm trying to avoid usin ...