PHP Scraper循环直到超过时间限制(使用简单HTML Dom解析器)

问题描述 投票:2回答:1

我已经在学习PHP时写了我的第一个脚本,试图从两个不同的网站上获取同一产品的价格。当它提取正确的结果并打印出我想要的内容时,当我运行脚本时,在正确打印结果后出现此错误:

致命错误:第144行[...]超过120秒的最大执行时间

“这是错误的照片”“>

这是我的代码:

<?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];
?>

我已经在学习PHP时写了我的第一个脚本,试图从两个不同的网站上获取同一产品的价格。当它得到正确的结果并打印出我想要的内容时,当I ...

php html dom web-scraping html-parsing
1个回答
0
投票

似乎您只对每个($cbdPrices[0])的一个价格感兴趣,而不是对价格的数组感兴趣,因此请在获得第一个价格后尝试打破循环。

© www.soinside.com 2019 - 2024. All rights reserved.