2013-07-25 118 views
0

我是cURL的新手。 我一直在試圖把this amazon link的內容(即圖像,書名,作者和20本書的價格)轉換爲html頁面。到目前爲止,我得到的是使用下面的代碼打印頁面使用PHP和cURL刮div的內容

<?php 
function curl($url) { 
    $options = Array(
     CURLOPT_RETURNTRANSFER => TRUE, 
     CURLOPT_FOLLOWLOCATION => TRUE, 
     CURLOPT_AUTOREFERER => TRUE, 
     CURLOPT_CONNECTTIMEOUT => 120, 
     CURLOPT_TIMEOUT => 120, 
     CURLOPT_MAXREDIRS => 10, 
     CURLOPT_URL => $url, 
    ); 

    $ch = curl_init(); 
    curl_setopt_array($ch, $options); 
    $data = curl_exec($ch); 
    curl_close($ch); 
    return $data; 
} 
?> 

$url = "http://www.amazon.in/gp/bestsellers/books/1318209031/ref=zg_bs_nav_b_2_1318203031"; 
$results_page = curl($url); 
echo $results_page; 

我試過使用正則表達式並失敗;我已經嘗試了所有可能的直接6小時,並非常疲倦,希望我會在這裏找到解決方案;只是感謝解決方案,但提前tq是不夠的。 :)

UPDATE:找到一個像我這樣的初學者一個真正有用的網站(click here)(不使用捲曲雖然)。

+0

有沒有你不使用該API的理由嗎?它會容易得多。 –

+1

對初學者使用DOMDocument,XPath,phpquery,simple_html_dom。請不要正則表達式。 – DevZer0

+0

http://docs.aws.amazon.com/AWSECommerceService/latest/DG/TopSellers.html :) –

回答

1

你真的應該使用AWSECommerce API,但這裏有一個辦法,把槓桿雅虎YQL服務:

<?php 
$query = sprintf(
    'http://query.yahooapis.com/v1/public/yql?q=%s', 
    urlencode('SELECT * FROM html WHERE url = "http://www.amazon.in/gp/bestsellers/books/1318209031/ref=zg_bs_nav_b_2_1318203031" AND xpath=\'//div[@class="zg_itemImmersion"]\'') 
); 

$xml = new SimpleXMLElement($query, null, true); 

foreach ($xml->results->div as $product) { 
    vprintf("%s\n", array(
     $product->div[1]->div[1]->a, 
    )); 
} 

/* 
    Engineering Thermodynamics 
    A Textbook of Fluids Mechanics 
    The Design of Everyday Things 
    A Forest History of India 
    Computer Networking 
    The Story of Microsoft 
    Private Empire: ExxonMobil and Americ... 
    Project Management Metrics, KPIs, and... 
    Design and Analysis of Experiments: I... 
    IES - 2013: General English 
    Foundation of Software Testing: ISTQB... 
    Faster: 100 Ways to Improve your Digi... 
    A Textbook of Fluid Mechanics and Hyd... 
    Software Engineering for Embedded Sys... 
    Communication Skills for Engineers 
    Making Things Move DIY Mechanisms for... 
    Virtual Instrumentation Using Labview 
    Geometric Dimensioning and Tolerancin... 
    Power System Protection & Switchgear... 
    Computer Networks 
*/ 
+0

我不能夠感謝你......所有人都可以做的就是標記有用的答案和+1 ... tq – John