PHP Scraper Loops Until Time Limit Exceeded (Using Simple HTML Dom Parser)-ThrowExceptions

Exception or error:

I am attempting to scrape the price of the same product from two different websites. While it pulls the correct results and prints out what I want, when the I run the script I get this error after the results are correctly printed:

Fatal error: Maximum execution time of 120 seconds exceeded in […] on line 144

Here's a photo of the error

And this is my code:

<?php
//Adds in the simple HTML DOM parser
include ('simple_html_dom.php');

//Defines the Target URL to Scrape
$cbdUrl = "https://cbdstore.co.za/product/africanpure-everyday-cbd-1000mg-30ml/";
$apUrl = "https://africanpure.co/product/everyday-cbd-oil-1000mg-30ml/";

//Defines 'html' as the scraped content from the URL above
$cbdHtml = file_get_html($cbdUrl);
$apHtml = file_get_html($apUrl);

//Creating an array to store all the 'price' classes text from the page     
$cbdPrices = array();

//Fetching all the '.amount' and storying them in the array as plain text.
foreach($cbdHtml->find('div.summary.entry-summary p.price') as $cbdElement)
{
    foreach($cbdElement->find('.amount') as $cbdAmt)
    {
        $cbdPrices [] = $cbdAmt->plaintext;
    }
}   

//Repeating for AfricanPure
$apPrices = array();

foreach($apHtml->find('div.summary-inner div.basel-scroll-content p.price') as $apElement)
{
    foreach($apElement->find('.amount') as $apAmt)
    {
        $apPrices [] = $apAmt->plaintext;
    }
}       


// Writes out CBD Store Price
echo 'CBD Store has the Everday CBD Oil for: ' . $cbdPrices[0];

// Writes out AP Price
echo 'African Pure has the Everday CBD Oil for: ' . $apPrices[0];
?>
How to solve:

It looks like you are only interested in one price for each ($cbdPrices[0]) and not an array of prices, so try breaking out of the loops after getting the first price.

foreach($cbdHtml->find('div.summary.entry-summary p.price') as $cbdElement)
{
    foreach($cbdElement->find('.amount') as $cbdAmt)
    {
        $cbdPrices [] = $cbdAmt->plaintext;
        break;
    }
}

And do the same on the other one. You could also probably not make the variable an array in the first place?

Leave a Reply

Your email address will not be published. Required fields are marked *