【问题标题】:Php cURL Web Scrapingphp cURL 网页抓取
【发布时间】:2015-01-27 11:07:30
【问题描述】:

我想从网站 url 上抓取手机的价格:http://www.flipkart.com/apple-iphone-5s/p/itmdv6f75dyxhmt4?pid=MOBDPPZZDX8WSPAT

如果查看代码,价格放在下面的SPAN中

<div class="pricing line">
        <div class="prices" itemprop="offers" itemscope="" itemtype="http://schema.org/Offer">
                    <div>
                        <span class="selling-price omniture-field" data-omnifield="eVar48" data-eVar48="37500">Rs. 37,500</span> // Fetch this price
                    </div>
                    <span class="sticky-message">Selling Price</span>
            <meta itemprop="price" content="37,500"> 
            <meta itemprop="priceCurrency" content="INR">
        </div>
</div>

到目前为止,我获取此代码的代码是:

<?php
$curl = curl_init('http://www.flipkart.com/apple-iphone-5s/p/itmdv6f75dyxhmt4?pid=MOBDPPZZDX8WSPAT');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

$page = curl_exec($curl);

if(!empty($curl)){ //if any html is actually returned

    $pokemon_doc->loadHTML($curl);
    libxml_clear_errors(); //remove errors for yucky html

    $pokemon_xpath = new DOMXPath($pokemon_doc);

    //get all the h2's with an id
    $pokemon_row = $pokemon_xpath->query('//h2[@id]');

    if($pokemon_row->length > 0){
        foreach($pokemon_row as $row){
            echo $row->nodeValue . "<br/>";
        }
    }
}

else
    print "Not found";
?>

这显示一个错误:

致命错误:在非对象上调用成员函数 loadHTML() D:\xampp\htdocs\jiteen\php-scrape\phpScrape.php 在第 9 行

我该怎么办,我无法追踪错误

【问题讨论】:

  • 我可以建议simplehtmldom.sourceforge.net 相信我,它很棒。并且非常易于使用。
  • 您好@kkaosninja,感谢您的帮助和时间。但老实说,我不太能够满足我的要求(可能是因为我没有仔细阅读文档)。你能建议我一个短而快的方法吗?此外,代码很难理解我从那里下载的文件:simple_html_dom.html。

标签: php html xpath web-scraping domdocument


【解决方案1】:

首先,您忘记实例化 DOMDocument 类,(至少在您在这个问题中的代码上)。

$curl = curl_init('http://www.flipkart.com/apple-iphone-5s/p/itmdv6f75dyxhmt4?pid=MOBDPPZZDX8WSPAT');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

$page = curl_exec($curl);

if(!empty($curl)) { //if any html is actually returned

    $pokemon_doc = new DOMDocument;
    libxml_use_internal_errors(true);
    $pokemon_doc->loadHTML($page);
    libxml_clear_errors();

    $pokemon_xpath = new DOMXPath($pokemon_doc);

    $price = $pokemon_xpath->evaluate('string(//div[@class="prices"]/meta[@itemprop="price"]/@content)');
    echo $price;

    $rupees = $pokemon_xpath->evaluate('string(//div[@class="prices"]/div/span)');
    echo $rupees;
}
else {
    print "Not found";
}

Sample Output

【讨论】:

  • 感谢您的宝贵时间和建议。它删除了错误,但现在它是一个空白页面,我没有所需的输出,即来自 URL 的价格。也请帮助我。
  • @Jiteen 到底是什么价格?卢比?
  • 是的,单位为卢比。我相信这是我们可以根据“$”符号搜索的美元,我猜。您可能已经注意到,我只想获取以下行中的值:** Rs. 37,500**
  • @Jiteen 哦好吧,和我想的一样,看看我的修改,还有一个示例输出
  • 这很好用。抱歉再次打扰您,我也想获取 SnapDeal 的价格。检查此链接:snapdeal.com/product/apple-iphone-5s-16-gb/… 价格在于:36920 我想知道如何更改 $price = $pokemon_xpath->评估('string(//div[@class="prices"]/meta[@itemprop="price"]/@content)');获取所需的数据。
猜你喜欢
  • 2015-02-27
  • 2014-11-01
  • 2017-11-20
  • 1970-01-01
  • 2018-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多