我可以网页抓取一个成功的报纸网站,但今天失败。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