2013-09-26 45 views
1

我試圖從網站中刪除一些內容,但我遇到了一個可能微不足道的問題,但找不到解決方案。對於第一頁,它的作品,但是當我瀏覽(捲曲)下面的頁面時,我仍然得到頁面1的內容,這很奇怪。我想在網站有一定的保護殺,但我無法找到一個方法來識別它們...cURL不適用於特定網站的分頁

<?php 
$i = 1; 
$links = array(); 

while($i < 3) 
{ 
    $ch = curl_init(); 
    $url = 'http://www.gites-de-france.com/location-vacances-chambre-hotes.html?page=$i&chambre=o&xhtml=O&acc=CHAMBRE,CHAMBRE&order_by=prix&order_by_tri=asc&'; 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_REFERER, "http://www.google.fr/"); 
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0"); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 100); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 

    $html = curl_exec($ch); 
    curl_close($ch); 

    $doc = phpQuery::newDocument($html); 

    foreach($doc['.vignette a'] as $a){ 
     $url = ''; 
     $links[] .= pq($a)->attr('href'); 
    } 
$i++; 
} 
    print_r($links); 
?> 

回答

3

這對我有效。

$i = 1; 
$links = array(); 
$baseUrl = "http://www.gites-de-france.com/location-vacances-chambre-hotes.html"; 
$param = array(
    'chambre' => 'o', 
    'xhtml' => 'O', 
    'acc' => 'CHAMBRE,CHAMBRE', 
    'order_by' => 'prix', 
    'order_by_tri' => 'asc' 
); 

while($i < 3) { 
    $ch = curl_init(); 

    $param['page'] = $i; 
    $url = "{$baseUrl}?" . http_build_query($param); 

    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_REFERER, "http://www.google.fr/"); 
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0"); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 100); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); 

    $html = curl_exec($ch); 
    curl_close($ch); 

    $doc = phpQuery::newDocument($html); 

    foreach($doc['.vignette a'] as $a){ 
     $url = ''; 
     $links[] .= pq($a)->attr('href'); 
    } 
    $i++; 
} 
print_r($links); 

注:我在運行腳本之前手動創建了cookie.txt文件。

+0

是的訣竅是使用cookies! –

0

你需要做象下面這樣:

/** 
* 
* 
* int $start start page number 
* int $limit maximum number of results 
* int $pgIncrmnt number of results per page 
* 
* 
*/ 
$buffer = NULL; 
$limit = 100; 

for ($j = $startPageNum; $j <= $limitMaxResult; $j = $j + $pgIncrmnt) { 
    $chr = curl_init(); 
    curl_setopt($chr, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.6 (KHTML, like Gecko) Chrome/16.0.897.0 Safari/535.6'); 
    curl_setopt($chr, CURLOPT_HEADER, FALSE); 
    curl_setopt($chr, CURLOPT_URL, 'http://www.windowsphone.com/'); 
    curl_setopt($chr, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($chr, CURLOPT_FRESH_CONNECT, TRUE); 
    curl_setopt($chr, CURLOPT_FORBID_REUSE, TRUE); 
    curl_setopt($chr, CURLOPT_FOLLOWLOCATION, TRUE); 

    $buffer .= curl_exec($chr); 
    curl_close($chr); 
} 
+0

感謝您的意見,我嘗試實施您的建議,如http://pastebin.com/cY2vbJyV我會錯過什麼嗎? –

+0

我可以看到第22行的問題 $ html = curl_exec($ ch); 你應該在這裏使用連接運算符來獲取var $ html中的完整輸出。 $ html。= curl_exec($ ch); 如果有幫助,您可以將答案分數提高1 ;-) –

+0

感謝您的更正,但並未解決問題;)您是否認爲他們使用會話Cookie來驗證url中頁面參數的使用? –

1

因此,這裏的解決方案,本網站使用cookies來傳遞會話號碼,因此您必須使用以下代碼

curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookie.txt'); 
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookie.txt'); 

它現在可以工作!