2015-05-29 132 views
2

我想刮這個頁面來獲得標準交付的運費。它沒有顯示結果。我嘗試過用不同的方法來分隔特殊字符。也許會有更好的方法來挑選標準成本價格。任何建議將是有益的。謝謝!PHP 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

您可以構建一個類象下面這樣:

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(); 

我希望這將滿足您的需求。

+0

這是好東西。 Array([0] => $ 75.00 [1] => $ 40.00 [2] => $ 0.00 [3] => $ 90.00)。但是,我將如何獲得標準td而不是所有的td?或者是否有辦法讓更多的聯想文章說Exp,2nd,Standard等? – user3549135

+0

@ user3549135你可以隨心所欲解決問題。 – Alex

+0

這實際上返回了整個以 user3549135