PHP preg_match 未显示结果
PHP preg_match not showing results
我正在尝试抓取此页面以仅获取标准配送的运费。它没有显示结果。我尝试了不同的技术来分隔特殊字符。或许有更好的方法来挑出标准成本价。任何建议都是 helpful.thanks!
<?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);
?>
你可以像下面这样构造一个 class :
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;
}
并在 class 中添加另一个函数:
function get() {
$content = $this->getContent();
if(is_array($content) and !empty($content)){
echo "<pre>";
print_r($content);
}
}
你可以这样称呼它:
$curl = new amazon;
$curl->get();
希望这能满足您的需求。
我正在尝试抓取此页面以仅获取标准配送的运费。它没有显示结果。我尝试了不同的技术来分隔特殊字符。或许有更好的方法来挑出标准成本价。任何建议都是 helpful.thanks!
<?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);
?>
你可以像下面这样构造一个 class :
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;
}
并在 class 中添加另一个函数:
function get() {
$content = $this->getContent();
if(is_array($content) and !empty($content)){
echo "<pre>";
print_r($content);
}
}
你可以这样称呼它:
$curl = new amazon;
$curl->get();
希望这能满足您的需求。