[英]PHP5 cURL - When attempting to scrape a page, it loads a blank page
我正在嘗試從頁面上刮一些食譜以用作學校項目的樣本,但是該頁面一直在加載空白頁面。
我正在關注本教程- 這里
這是我的代碼:
<?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
。 我將不勝感激任何幫助。
這是問題所在:
$results_page = curl($url);
您嘗試不是從URL而是從HTML頁面獲取內容。 因為在while()
之前,您將$url
設置$url
頁面的結果。 我認為您應該執行以下操作:
$results_page = curl("https://www.justapinch.com/recipes/main-course/");
編輯:
您應該將查詢html的方式更改為使用DOM 。
人們為什么這樣做? 代碼完全沒有錯誤檢查,然后他們去某個論壇,問why is this code, which completely ignores any and all errors, not working?
我不知道,但是至少您可能會提出一些錯誤檢查並在請求之前運行它。 不僅是您,很多人都在這樣做,而且煩人的事,您都應該為此感到難過。 如果在設置選項時出錯,curl_setopt返回bool(false)。 如果傳輸中有錯誤,curl_exec返回bool(false)。 如果創建卷曲句柄時出錯,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 ();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.