curl - PHP preg_match not showing results -
i trying scrape page shipping cost standard delivery only. not showing results. have tried different techniques delimiting special characters. perhaps there better approach single out standard cost price. suggestions 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 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); ?>
you can construct class below :
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();
edit:
you can change getcontent()
function :
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 $key => $val) { if (!empty($val) && (!preg_match('/total shipping cost item/i',$val)) && (!preg_match('/continental street/i',$val))){ $current[$key] = strip_tags($val); } } return $current; }
and add function class :
function get() { $content = $this->getcontent(); if(is_array($content) , !empty($content)){ echo "<pre>"; print_r($content); } }
than can call :
$curl = new amazon; $curl->get();
i hope fit needs.
Comments
Post a Comment