2014-12-31 203 views
-4

我可以网页抓取一个成功的报纸网站,但今天失败。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来成功地假装浏览器?我错过了一些参数吗?

+6

它清楚地表明拒绝访问。未经允许您不得抓取网站,并且您被禁止。 – FrEaKmAn

+0

但我可以通过使用firefox成功访问网站“http://www1.hkej.com/dailynews/toc”。它只是发生在卷曲中。 – Fireghost

+0

因为它基于浏览器签名进行阻止(签名可能是由不同的参数构建的)。您的默认Firefox拥有与curl不同的签名。 – FrEaKmAn

回答

2

正如我在文章中说,我可以使用Firefox来访问Web和我的IP没有被禁止。 最后,我成功以后,我从

$agent = "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:33.0) Gecko/20100101 Firefox/33.0"; 

改变了代码

$agent = $_SERVER['HTTP_USER_AGENT']; 

其实,我不知道它为什么会失败时“的User-Agent:”存在从昨天开始,但这是没事的。

感谢所有反正。

+1

就像我建议:)请标记你回答“关闭”的问题 – Scriptable

1

用户已经使用Cloudflares安全功能,以防止你抓取他们的网站,更可能得到显示为恶意的僵尸。他们将根据您的用户代理和IP地址完成此操作。

试着改变你的IP(如家庭用户,请尝试重新启动路由器。有时会得到不同的IP地址)。尝试使用代理并尝试使用Curl发送不同的标题。

更重要的是,他们不希望人们抓取他们的网站,并影响其业务等,你真的应该征得同意这一点。