我可以網頁抓取一個成功的報紙網站,但今天失敗。PHP curl網頁抓取突然失敗
但我可以用firefox成功地訪問網絡。它只是發生在捲曲中。這意味着它允許我的IP訪問並且不被禁止。
這裏是由網絡
請,啓動cookies所示的錯誤。
錯誤1010光線ID:1a17d04d7c4f8888
拒絕訪問
發生了什麼?
本網站所有者(www1.hkej.com)根據您的瀏覽器簽名(1a17d04d7c4f8888-ua45)禁止您訪問 。
CloudFlare的光線ID:1a17d04d7c4f8888•您IP:2xx.1x.1xx.2xx• 性能&安全通過CloudFlare的
這裏是我的代碼前工作:
$cookieMain = "cookieHKEJ.txt"; // need to use 2 different cookies since it will overwrite the old one when curl store cookie. cookie file is store under apache folder
$cookieMobile = "cookieMobile.txt"; // need to use 2 different cookies since it will overwrite the old one when curl store cookie. cookie file is store under apache folder
$agent = "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:33.0) Gecko/20100101 Firefox/33.0";
// submit a login
function cLogin($url, $post, $agent, $cookiefile, $referer) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 100); // follow the location if the web page refer to the other page automatically
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Get returned value as string (don’t put to screen)
curl_setopt($ch, CURLOPT_USERAGENT, $agent); // Spoof the user-agent to be the browser that the user is on (and accessing the php script)
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile); // Use cookie.txt for STORING cookies
curl_setopt($ch, CURLOPT_POST, true); // Tell curl that we are posting data
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); // Post the data in the array above
curl_setopt($ch, CURLOPT_REFERER, $referer);
$output = curl_exec($ch); // execute
curl_close($ch);
return $output;
}
$input = cDisplay("http://www1.hkej.com/dailynews/toc", $agent, $cookieMain);
echo $input;
哪有我使用curl來成功地假裝瀏覽器?我錯過了一些參數嗎?
它清楚地表明拒絕訪問。未經允許您不得抓取網站,並且您被禁止。 – FrEaKmAn
但我可以通過使用firefox成功訪問網站「http://www1.hkej.com/dailynews/toc」。它只是發生在捲曲中。 – Fireghost
因爲它基於瀏覽器簽名進行阻止(簽名可能是由不同的參數構建的)。您的默認Firefox擁有與curl不同的簽名。 – FrEaKmAn