2014-12-31 201 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發送不同的標題。

更重要的是,他們不希望人們抓取他們的網站,並影響其業務等,你真的應該徵得同意這一點。