Reputation: 2200
I'm using Simple HTML DOM to get elements and values from a website I'm scraping. It works great, but every 23,000 pages or so, I hit the equivalent of a 404 page not found, in which the normal html structure is not present. This results in a fatal error I want to escape without breaking the page.
Here is the full error report:
Fatal error: Uncaught Error: Call to a member function find() on null in C:\xampp\htdocs\scrape\simple_html_dom.php:1113 Stack trace: #0 C:\xampp\htdocs\scrape\scrape_detailtable.php(133): simple_html_dom->find('div[id=detailta...') #1 {main} thrown in C:\xampp\htdocs\scrape\simple_html_dom.php on line 1113
Here are the lines in my script where the error occurs:
$doc = new simple_html_dom($record_content);
if ( ! is_null($doc->find("div[id=detailtable]")) ) // <-- this line
$record_content
is the html retrieved using a cURL.
I've tried
if( ! isset($doc->find("div[id=detailtable]")) ) ,
if( ! is_null($doc->find("div[id=detailtable]")) ) ,
if( ! empty($doc->find("div[id=detailtable]")) ) , and ,
if( ! $doc->find("div[id=detailtable]") ) ...
...and the page keeps breaking. I just want to escape processing on condition of the existence of div[id=detailtable].
update on 6/30/2016
I got another PHP error trying different escape and "detect not null" methods (Maybe this is new with PHP 7). Eventually I tried something where PHP threw this suggestion, along with the error: (paraphrasing) "You can use null !== expression"
So now my code reads: if ( null !== $doc->find( 'a' ) ){
Blah! still getting "Uncaught Error: Call to a member function find() on null" so I guess null !== expression doesn't really work.
Upvotes: 1
Views: 3837
Reputation: 399
Try something like that:
$doc = new simple_html_dom($record_content);
if ( $doc ) {
if ( ($find = $doc->find("div[id=detailtable]")) ) {
// do what you want with the $find variable
}
if ( ($find = $doc->find("div[id=detailtable2]")) ) {
// do what you want with the $find variable
}
if ( ($find = $doc->find("div[id=detailtable3]")) ) {
// do what you want with the $find variable
}
}
Upvotes: 1