无法获得所有跨度值

时间:2015-07-01 11:45:36

标签: php curl preg-match preg-match-all

$url = "http://www.amazon.in/gp/product/B00UMSAWOY/";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
curl_exec($ch);
curl_close($ch);

preg_match_all("/<span>(.*)<\/span>/", $result, $matches);
print_r($matches);
foreach($matches[1] as $val)
{
    echo $val."<br>";
}

如果我运行此查询,我的结果为996-2015, Amazon.com, Inc. or its affiliates。但我需要链接中可用的所有跨度值。我想要打印的是产品名称和价格。任何人都可以帮忙解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

 <?php
 $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.amazon.in/gp/product/B00UMSAWOY/');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$html = curl_exec($ch);
curl_close($ch);    

 $dom = new DOMDocument;
 @$dom->loadHTML($html);
 $xpath = new DOMXPath($dom);
$searchNode = $dom->getElementsByTagName( "span" ); 
 foreach ($dom->getElementsByTagName('span') as $tag)                  
{
//echo $tag->getAttribute('id');
if($tag->getAttribute('id')=='productTitle')
{
    echo $tag1=$tag->nodeValue . '<br/>';
}
if($tag->getAttribute('id')=='priceblock_saleprice')
{
    echo $tag1=$tag->nodeValue . '<br/>';
}
}
?>

我找到了解决方案的朋友。谢谢你的帮助

答案 1 :(得分:0)

下面的代码将解决您的问题

preg_match_all('/<span>.*?<\/span>/is', $result, $matches);
相关问题