使用PHP简单HTML DOM解析器时发生致命错误

cigdeys3  于 2023-01-01  发布在  PHP
关注(0)|答案(1)|浏览(157)

希望你一切都好。抱歉我肯定这是个愚蠢的问题...
Started working on using a PHP/HTMl scraper, and was following the tutorial here: https://www.zenrows.com/blog/web-scraping-php#introduction
制定了如何使用composer,安装依赖项等,但当使用的例子,并发挥它周围,我得到以下:
致命错误:未捕获的类型错误:传递给voku\helper\HtmlDomParser::loadHtml()的参数1必须是字符串类型,给定布尔值
我已经尝试了几乎所有我能想到的,这是违规代码:

require_once __DIR__ . '/../vendor/autoload.php';
    require_once  __DIR__ . '/constants.php';
    require_once  __DIR__ . '/utils/scraping.php';
    use voku\helper\HtmlDomParser;
    
    // initializing the cURL request
    $curl = curl_init();
    // setting the URL to reach with a GET HTTP request
    curl_setopt($curl, CURLOPT_URL, "https://scrapeme.live/shop/");
    // to make the cURL request follow eventual redirects
    // and reach the final page of interest
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    // to get the data returned by the cURL request as a string
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    // setting the User-Agent header
    curl_setopt($curl, CURLOPT_USERAGENT, USER_AGENT);
    // executing the cURL request and
    // get the HTML of the page as a string
    $html = curl_exec($curl);
    // releasing the cURL resources
    curl_close($curl);
    
    echo gettype ($html);
    echo "here";
    echo $html;
    
    // initialize HtmlDomParser 
    
    
    // initializing HtmlDomParser
    $htmlDomParser = HtmlDomParser::str_get_html($html);

我已经检查了$html的var类型,PHP正在回应它是一个字符串。
再次为我的密集感到抱歉,很高兴做阅读,但有没有人能给我指出正确的方向?
谢谢!

efzxgjgh

efzxgjgh1#

出错时cURL可能返回布尔值。从docs:“成功时返回结果,失败时返回false。"。我也运行了代码,它失败了一次,另一次正常。也许你可以检查值,如果有错误,重试请求。

相关问题