2011-02-24 53 views
1

我想寫一個刮刮臉的應用程序,我遇到了問題。我的PHP Curl代碼不會以書籍的價格拉動頁面。它將我返回到域的Web根目錄。刮書價格

我在努力按ISBN搜索網站。

我一直在撞牆撞牆。任何幫助將不勝感激!

代碼:

<form method="post" for="new-search" name="SearchTerm" class='form-validate' id="SearchTerm" action="index.php"> 
    <textarea rows="3" name="SearchTerm" id="SearchTerm" cols="40" class="validate-required error"></textarea><div class="error" id="SearchTerm-error"> 
    <br>       
    <button class="search primary" type="submit">continue</button> 

</form> 


<?php 

/* 
echo("<pre>");print_r($_GET);echo("</pre>"); 
echo("<pre>");print_r($_POST);echo("</pre>"); 
*/ 

$isbn = $_POST['SearchTerm']; 


$userAgent = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16'; 

$fields = array(
    'url' => ("http://www.bookleberry.com/Search/SearchKeyword"), 
    'qurl' => ("http://www.bookleberry.com/Search/SearchKeyword/" . $_POST['SearchTerm']), 
    'SearchTerm' => ($_POST['SearchTerm']), 
    'Page' => ('1'), 
    'class' => ('textfield validate-required'), 
    'for' => ('new-search'), 
    'result-count' => ('1'), 
    'status' => 'success', 
); 

$SearchTerm = ($fields['SearchTerm']); 
$url = ($fields['url']); 
$Page = ($fields['Page']); 


echo("<pre>"); 
print_r($fields); 
echo("</pre>"); 

if ($isbn != NULL){ 

    //open connection 
    $ch = curl_init($url); 
    //set the url, number of POST vars, POST data 
    curl_setopt($ch, CURLOPT_HEADER, $userAgent); 
    curl_setopt($ch, CURLOPT_URL, $url); 
     echo "before curl_exec:<br>"; 
     echo "curl_errno=". curl_errno($ch) ."<br>"; 
     echo "curl_error=". curl_error($ch) ."<br>"; 
    curl_setopt($ch,CURLOPT_POST,count($fields)); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, "?SearchTerm=$SearchTerm"); 
    curl_setopt($ch, CURLOPT_HTTPGET, 1); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 9999999); 
    curl_setopt($ch,CURLOPT_HTTPHEADER,array (
     "Accept: application/json" 
    )); 




    $info = curl_getinfo($ch); 

    //execute post 
    $result = curl_exec($ch); 
    print $result; 


print "<pre>\n"; 
print_r(curl_getinfo($ch)); // get error info 

?> 
+1

副手我會說,因爲內容似乎是AJAX填充。用PHP/CURL刮不會讓你走得太遠,你需要攔截AJAX調用,並獲得javascript在後臺使用的結果。 – 2011-02-24 19:22:06

回答

4

不傷你的頭,用它!

  • 安裝fiddler
  • 使用瀏覽器做一個請求,看看fiddler到底是什麼發佈。這包括所有標題,cookie和表單變量。
  • 使用您的代碼做一篇文章,再次檢查提琴手
  • 比較兩者之間的差異並調整您的腳本。
  • 重複。

另外它有助於安裝firebug。使用複製Xpath,並將其放入一個php DOM xpath查詢使得刮樂趣和輕鬆!

+0

我喜歡網頁截圖的DOM Xpath查詢想法 – emaillenin 2011-02-25 17:32:20

+0

@emailenin - 請記住刪除螢火蟲放入的元素 – 2011-02-25 17:36:36

+0

您提供的步驟是我在抓取網站時總是使用的步驟; – 2011-02-26 04:58:17