我有一个基于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";
}
?>
我没有收到任何错误,也没有显示任何数据(价格)。我无法追踪任何错误。
答案 0 :(得分:0)
我做了一个非常愚蠢的错误:添加一个额外的'/'解决了这个问题。感谢@DaveCoast。新代码是
<?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";
}
?>
希望这有助于某人!