使用来自不同域名网址的php curl抓取数据

时间:2014-12-17 06:57:04

标签: php curl

我需要使用php preg match从不同域名网址中获取价格我面临问题价格提取但显示错误的价格。在这里,我需要从网址获取flipkart,亚马逊价格。 get_page函数是curl来获取数据。

代码

<?php
$urls=array('http://www.flipkart.com/moto-e/p/itmdvuwsybgnbtha?pid=MOBDVHC6XKKPZ3GZ','http://www.amazon.in/gp/product/B00Q2HGF3Q');

foreach($urls as $url)
{
//flipkart
if(preg_match("/flipkart\.[com|in]/is",$url))
{
    $data['type'] = "flipkart.in";

   if(preg_match("/class=\"price[\s]fk-display-block\">Rs\.(.*?)<\/span>/is", get_page($url), $m))
    {
        $m[1] = strtolower($m[1]);
        $data['price'] = (float) str_replace(array('rs.', ','),'',$m[1]);

    }
    elseif(preg_match("/data\-evar48=\"([0-9+]+?)\"/is", get_page($url), $m))
    {
    $m[1] = strtolower($m[1]);
    $data['price'] = (float) str_replace(array('rs.', ','),'',$m[1]);
    }

}
//amazon
    if(preg_match("/amazon\.in/is",$url))
    {
    $data['type'] = "amazon.in";
    if(preg_match("/class=\"currencyINR\"[\s]style=\"display:none\">Rs.[\s]<\/span>(.*?)<\/span>/is", get_page($url), $m))
    {
    $m[1] = strtolower($m[1]);
    $data['price'] = (float) str_replace(array('rs.', ','),'',$m[1]);
    }
    }

var_dump($data);

}



?>

2 个答案:

答案 0 :(得分:3)

尝试使用flipkart价格:

preg_match('/<span class="selling-price.*?data-eVar48="(.*?)">/',$sourcestring,$matches);
echo $matches[1];

答案 1 :(得分:2)

试试这个新代码,这将分别给出flipkart和amazon的价格:

<?php
$urls=array('http://www.flipkart.com/moto-e/p/itmdvuwsybgnbtha?pid=MOBDVHC6XKKPZ3GZ','http://www.amazon.in/gp/product/B00Q2HGF3Q');

foreach($urls as $key => $url)
{
//flipkart

if(preg_match("/flipkart\.[com|in]/is",$url))
{
    $data[$key]['type'] = "flipkart.in";

   if(preg_match('/<span class="selling-price.*?data-eVar48="(.*?)">/',get_page($url),$matches))
    { 
        $data[$key]['price']=$matches[1];
    }

}
//amazon
    if(preg_match("/amazon\.in/is",$url))
    {
        $data[$key]['type'] = "amazon.in";
        if(preg_match('/<span class="currencyINR">&nbsp;&nbsp;<\/span>(.*?)<\/span>/', get_page($url),$matches1))
        {
            $data[$key]['price']=$matches1[1];
        }
    }



}

var_dump($data);
?>