PHP preg_match没有显示结果

时间:2015-05-29 14:52:51

标签: php curl preg-match

我正在尝试抓取此页面以获取标准投放的运费。它没有显示结果。我尝试过不同的技术来分隔特殊字符。也许会有更好的方法来挑出标准成本价格。任何建议都会有所帮助。谢谢!

<?php
$curl = curl_init();
$url = 'http://www.amazon.com/gp/aag/details?ie=UTF8&asin=B009S7L8A2&isAmazonFulfilled=&isCBA=&marketplaceID=ATVPDKIKX0DER&orderID=&seller=A1N0I0RBOCG9TK&sshmPath=shipping-rates#/aag_shipping';

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );   
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$resp = curl_exec($curl);

//echo $resp;


function amazon_scrape($html) {
preg_match(
        '/<td class="tiny" nowrap="nowrap" width="20%"><strong> Standard <\/strong> <\/td><td class="tiny" align="right"><table bgcolor="#F4F4F4" ><tr bgcolor="#F4F4F4"><td class="tiny" width="200" nowrap="nowrap">Continental US Street <\/td><td  class="tiny" width="200" nowrap="nowrap">\$(.*?)<\/td>/s',

        $html,
        $price
    );

    return $price;
}
$price2 = amazon_scrape($resp);
echo $price2[0];
curl_close($curl);

?>

1 个答案:

答案 0 :(得分:1)

您可以构建如下所示的类:

class amazon {
    function curl($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
    function getContent() {
        $feed = "http://www.amazon.com/gp/aag/details?ie=UTF8&asin=B009S7L8A2&isAmazonFulfilled=&isCBA=&marketplaceID=ATVPDKIKX0DER&orderID=&seller=A1N0I0RBOCG9TK&sshmPath=shipping-rates#/aag_shipping";
        $content = $this->curl($feed);
        $content = strip_tags($content,'<TD>');
        preg_match_all('/<td  class="tiny" width="200" nowrap="nowrap">(?:[^>]*)<\/TD>/is',$content,$matches);

        print_r($matches[0]);
    }
}

$curl = new amazon;
$curl->getContent();

修改

您可以将getContent()功能更改为:

    function getContent() {
        $feed = "http://www.amazon.com/gp/aag/details?ie=UTF8&asin=B009S7L8A2&isAmazonFulfilled=&isCBA=&marketplaceID=ATVPDKIKX0DER&orderID=&seller=A1N0I0RBOCG9TK&sshmPath=shipping-rates#/aag_shipping";
        $content = $this->curl($feed);
        $content = strip_tags($content,'<DIV>');
        preg_match_all('/<DIV id="fix_expanded">(?:[^>]*)<\/DIV>/is',$content,$matches);
        $rows = explode("\n", $matches[0][0]);
        foreach($rows as $key => $val) {
            if (!empty($val) && (!preg_match('/Total Shipping Cost for This Item/i',$val)) && (!preg_match('/Continental US Street/i',$val))){
                $current[$key] = strip_tags($val);
            }
        }
        return $current;
    }

在课堂上添加另一个功能:

    function get() {
        $content = $this->getContent();
        if(is_array($content) and !empty($content)){
            echo "<pre>";
            print_r($content);
        }
    }

比你可以称之为:

$curl = new amazon;
$curl->get();

我希望这符合您的需求。