【问题标题】:PHP5 cURL - When attempting to scrape a page, it loads a blank pagePHP5 cURL - 尝试抓取页面时,它会加载一个空白页面
【发布时间】:2017-11-17 05:32:36
【问题描述】:

我正在尝试从页面上刮下一些食谱以用作学校项目的示例,但该页面一直在加载一个空白页面。

我正在关注本教程 - here

这是我的代码:

<?php

function curl($url) {
    $ch = curl_init();  // Initialising cURL
    curl_setopt($ch, CURLOPT_URL, $url);    // Setting cURL's URL option with the $url variable passed into the function
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Setting cURL's option to return the webpage data
    $data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable
    curl_close($ch);    // Closing cURL
    return $data;   // Returning the data from the function
}
function scrape_between($data, $start, $end){
    $data = stristr($data, $start); // Stripping all data from before $start
    $data = substr($data, strlen($start));  // Stripping $start
    $stop = stripos($data, $end);   // Getting the position of the $end of the data to scrape
    $data = substr($data, 0, $stop);    // Stripping all data from after and including the $end of the data to scrape
    return $data;   // Returning the scraped data from the function
}

$continue = true;

$url = curl("https://www.justapinch.com/recipes/main-course/");

while ($continue == true) {
    $results_page = curl($url);
    $results_page = scrape_between($results_page,"<div id=\"grid-normal\">","<div id=\"rightside-content\"");
    $separate_results = explode("<h3 class=\"tight-margin\"",$results_page);

    foreach ($separate_results as $separate_result) {
        if ($separate_result != "") {
            $results_urls[] = "https://www.justapinch.com" . scrape_between($separate_result,"href=\"","\" class=\"");
        }
    }

    // Commented out to test code above

    // if (strpos($results_page,"Next Page")) {
    //     $continue = true;
    //     $url = scrape_between($results_page,"<nav><div class=\"col-xs-7\">","</div><nav>");
    //     if (strpos($url,"Back</a>")) {
    //         $url = scrape_between($url,"Back</a>",">Next Page");
    //     }
    //     $url = "https://www.justapinch.com" . scrape_between($url, "href=\"", "\"");
    // } else {
    //     $continue = false;
    // }
    // sleep(rand(3,5));

    print_r($results_urls);
}
?>

我正在使用cloud9,并且我已经安装了php5 cURL,并且正在运行apache2。我将不胜感激。

【问题讨论】:

    标签: php html curl web-scraping


    【解决方案1】:

    这就是问题所在:

    $results_page = curl($url);
    

    您试图不是从 URL,而是从 HTML 页面获取内容。因为,就在while() 之前,您将$url 设置为页面的结果。我认为你应该做到以下几点:

    $results_page = curl("https://www.justapinch.com/recipes/main-course/");
    

    编辑:

    您应该将查询 html 的方式更改为using DOM

    【讨论】:

    • 那是因为文档中没有&lt;div id=\"rightside-content\"...
    【解决方案2】:

    为什么人们会这样做?代码完全没有错误检查,然后他们去某个论坛问why is this code, which completely ignores any and all errors, not working?我不知道,但至少你可以提出一些错误检查并在询问之前运行它。不只是你,很多人都在这样做,而且很烦人,你都应该为这样做感到难过。如果设置选项时出错,curl_setopt 返回 bool(false)。如果传输出错,curl_exec 返回 bool(false)。如果创建 curl 句柄时出错,curl_init 返回 bool(false)。使用 curl_error 提取错误描述,并使用 \RuntimeException 报告它。现在删除这个帖子,添加一些错误检查,如果错误检查没有发现问题,或者确实发现了问题但你不确定如何解决它,那么就创建一个关于它的新帖子。

    这里有一些错误检查函数包装器可以帮助您入门:

    function ecurl_setopt ( /*resource*/$ch , int $option , /*mixed*/ $value ):bool{
        $ret=curl_setopt($ch,$option,$value);
        if($ret!==true){
            //option should be obvious by stack trace
            throw new RuntimeException ( 'curl_setopt() failed. curl_errno: ' . return_var_dump ( curl_errno ($ch) ).'. curl_error: '.curl_error($ch) );
        }
        return true;
    }
    function ecurl_exec ( /*resource*/$ch):bool{
        $ret=curl_exec($ch);
        if($ret!==true){
            throw new RuntimeException ( 'curl_exec() failed. curl_errno: ' . return_var_dump ( curl_errno ($ch) ).'. curl_error: '.curl_error($ch) );
        }
        return true;
    }
    
    
    function return_var_dump(/*...*/){
        $args = func_get_args ();
        ob_start ();
        call_user_func_array ( 'var_dump', $args );
        return ob_get_clean ();
    }
    

    【讨论】:

    • 老兄,感谢您的帮助,但没有必要粗鲁。如果它惹恼了你就忽略它。对编码比较陌生的人可能会试图了解实际的代码,并且可能并不总是意识到错误检查可能会有很大帮助。我们都必须从某个地方开始,不是吗?你是一位经验丰富的程序员,你的技能超过了很多人,但这并不意味着可以粗鲁。
    猜你喜欢
    • 2011-08-11
    • 2012-12-20
    • 2022-07-16
    • 1970-01-01
    • 2013-07-08
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多