以下代码如何改为用 curl来抓取呢?并且以商品的IID保存
function get_shuxing($type,$iid){if($type=='tmall'){$text=file_get_contents("http://detail.tmall.com/item.htm?id=$iid");preg_match('|<ul id="J_AttrUL">(.*)</ul>|isU',$text, $match);}elseif ($type=='taobao'){$text=file_get_contents("http://item.taobao.com/item.htm?id=$iid");preg_match('|<ul class="attributes-list">(.*)</ul>|isU',$text, $match);}$match=$match[0];$match = iconv('gbk', 'utf-8', $match);echo $match;}
有哪位大神帮忙呢》
你不给测试数据怎么弄,再怎么也得给个$iid吧...
你不给测试数据怎么弄,再怎么也得给个$iid吧...
$html=file_get_contents('http://detail.tmall.com/item.htm?id=13317551791');include 'simple_html_dom.php';//网上下一个 $dom=new simple_html_dom();// $html utf8才可以$dom->load($html);$names=$dom->find('select[id=js]');$res=array();$lis=$dom->find('ul[id=J_AttrUL] li');foreach($lis as $k=>$li){ $temp=$li->text(); list($key,$value)=explode(':',$temp); $data[$key]=$value;}echo "<pre class="brush:php;toolbar:false">";print_r($data);echo "
$html=file_get_contents('http://detail.tmall.com/item.htm?id=13317551791');include 'simple_html_dom.php';//网上下一个 $dom=new simple_html_dom();// $html utf8才可以$dom->load($html);$names=$dom->find('select[id=js]');$res=array();$lis=$dom->find('ul[id=J_AttrUL] li');foreach($lis as $k=>$li){ $temp=$li->text(); list($key,$value)=explode(':',$temp); $data[$key]=$value;}echo "<pre class="brush:php;toolbar:false">";print_r($data);echo "
这些不是产品参数?数据都取出来了,至于怎么用,就看你自己处理,我又不是知道你要什么样的数据格式,一维,二维?键值对?
不要用正则,我记得抓这些属性有http请求方法的,你抓个包就知道了
这样吗?
function get_shuxing($type,$iid){ if($type=='tmall'){ //$text=file_get_contents("http://detail.tmall.com/item.htm?id=$iid"); $text=use_curl("http://detail.tmall.com/item.htm?id=$iid"); preg_match('|<ul id="J_AttrUL">(.*)</ul>|isU',$text, $match); }elseif ($type=='taobao'){ //$text=file_get_contents("http://item.taobao.com/item.htm?id=$iid"); $text=use_curl("http://item.taobao.com/item.htm?id=$iid"); preg_match('|<ul class="attributes-list">(.*)</ul>|isU',$text, $match); } $match=$match[0]; $match = iconv('gbk', 'utf-8', $match); echo $match;}function use_curl($url){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); return $output;}get_shuxing('taobao',45098460735);
这样吗?
function get_shuxing($type,$iid){ if($type=='tmall'){ //$text=file_get_contents("http://detail.tmall.com/item.htm?id=$iid"); $text=use_curl("http://detail.tmall.com/item.htm?id=$iid"); preg_match('|<ul id="J_AttrUL">(.*)</ul>|isU',$text, $match); }elseif ($type=='taobao'){ //$text=file_get_contents("http://item.taobao.com/item.htm?id=$iid"); $text=use_curl("http://item.taobao.com/item.htm?id=$iid"); preg_match('|<ul class="attributes-list">(.*)</ul>|isU',$text, $match); } $match=$match[0]; $match = iconv('gbk', 'utf-8', $match); echo $match;}function use_curl($url){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); return $output;}get_shuxing('taobao',45098460735);
这样吗?
function get_shuxing($type,$iid){ if($type=='tmall'){ //$text=file_get_contents("http://detail.tmall.com/item.htm?id=$iid"); $text=use_curl("http://detail.tmall.com/item.htm?id=$iid"); preg_match('|<ul id="J_AttrUL">(.*)</ul>|isU',$text, $match); }elseif ($type=='taobao'){ //$text=file_get_contents("http://item.taobao.com/item.htm?id=$iid"); $text=use_curl("http://item.taobao.com/item.htm?id=$iid"); preg_match('|<ul class="attributes-list">(.*)</ul>|isU',$text, $match); } $match=$match[0]; $match = iconv('gbk', 'utf-8', $match); echo $match;}function use_curl($url){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); return $output;}get_shuxing('taobao',45098460735);
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>302 Found</title></head><body bgcolor="white"><h1>302 Found</h1><p>The requested resource resides temporarily under a different URI.</p><hr/>Powered by Tengine</body></html>