我有一个基于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";
}
?>
我没有收到任何错误,也没有显示任何数据(价格)。我无法跟踪任何错误。
我犯了一个非常愚蠢的错误:添加额外的"/"解决了这个问题。感谢@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";
}
?>
希望这对某人有所帮助!