【发布时间】:2015-03-30 17:05:44
【问题描述】:
我有一个基于 cURL 的代码来从网站获取产品价格。我想从http://www.snapdeal.com/product/apple-iphone-5s-16-gb/1302850866抓取结果
价格放置在:
<div class="prodbuy-price">
<div id="mrp-price-outer" class="">
<div id="seller-price-outer" class="">
<div id="offer-price-id">
<meta content="INR" itemprop="priceCurrency">
<strong class="voucherPrice">
Rs
<span id="selling-price-id" itemprop="price">36500</span>
</strong>
我获取价格的代码是:
<?php
$curl = curl_init('http://www.snapdeal.com/product/apple-iphone-5s-16-gb/1302850866');
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(); //remove errors for yucky html
$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="prodbuy-price"]/span[@itemprop="price"])');
echo $rupees;
}
else {
print "Not found";
}
?>
我没有收到任何错误,也没有显示任何数据(价格)。我无法追踪任何错误。
【问题讨论】:
-
在 span 之前尝试一个额外的斜线以获得任何死者。一个斜线是直接死者。
-
是的,它运行良好。谢谢你,我可能没有发现那个愚蠢的错误。非常感谢!
标签: php curl web-scraping