【问题标题】:Scrape div contents using PHP and cURL使用 PHP 和 cURL 抓取 div 内容
【发布时间】:2013-07-25 12:30:33
【问题描述】:

我是 cURL 的新手。 我一直在尝试将this amazon link 的内容(即20 本书的图像、书名、作者和价格)刮到一个html 页面中。到目前为止,我已经使用以下代码打印页面

<?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。 :)

更新:为像我这样的初学者找到了一个非常有用的站点(click here)(尽管不使用 cURL)。

【问题讨论】:

  • 您没有为此使用 API 有什么原因吗?这会容易得多。
  • 对初学者使用 DOMDocument、XPath、phpquery、simple_html_dom。请不要正则表达式。
  • @DevZer0 正则表达式只是我的最后一个选择。经过这么长时间的尝试,我完全迷失了。无意冒犯,但解决方案将有助于提高我的地位。 tq DevZer0
  • 这也可能有点意思。 y.ahoo.it/NzKmy

标签: php regex curl


【解决方案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
*/

【讨论】:

  • 我非常感谢你...所有可以做的就是标记答案有用并再次+1...tq
猜你喜欢
  • 2015-10-11
  • 1970-01-01
  • 1970-01-01
  • 2012-01-14
  • 2014-01-04
  • 1970-01-01
  • 1970-01-01
  • 2019-09-03
  • 1970-01-01
相关资源
最近更新 更多