php file_get_contents 转curl抓取淘宝商品属性 大神帮忙下
本帖最后由 winniephp 于 2015-05-25 12:16:01 编辑 以下代码如何改为用 curl来抓取呢?并且以商品的IID保存
<br />function get_shuxing($type,$iid){<br />if($type=='tmall'){<br />$text=file_get_contents("http://detail.tmall.com/item.htm?id=$iid");<br />preg_match('|<ul id="J_AttrUL">(.*)</ul>|isU',$text, $match);<br />}elseif ($type=='taobao'){<br />$text=file_get_contents("http://item.taobao.com/item.htm?id=$iid");<br />preg_match('|<ul class="attributes-list">(.*)</ul>|isU',$text, $match);<br />}<br />$match=$match[0];<br />$match = iconv('gbk', 'utf-8', $match);<br />echo $match;<br />}
登录后复制
------解决思路----------------------这样吗?
function get_shuxing($type,$iid){<br /> if($type=='tmall'){<br /> //$text=file_get_contents("http://detail.tmall.com/item.htm?id=$iid");<br /> $text=use_curl("http://detail.tmall.com/item.htm?id=$iid");<br /> preg_match('<br><font color='#FF8000'>------解决思路----------------------</font><br><ul id="J_AttrUL">(.*)</ul><br><font color='#FF8000'>------解决思路----------------------</font><br>isU',$text, $match);<br /> }elseif ($type=='taobao'){<br /> //$text=file_get_contents("http://item.taobao.com/item.htm?id=$iid");<br /> $text=use_curl("http://item.taobao.com/item.htm?id=$iid");<br /> preg_match('<br><font color='#FF8000'>------解决思路----------------------</font><br><ul class="attributes-list">(.*)</ul><br><font color='#FF8000'>------解决思路----------------------</font><br>isU',$text, $match);<br /> }<br /> $match=$match[0];<br /> $match = iconv('gbk', 'utf-8', $match);<br /> echo $match;<br />}<br />function use_curl($url){<br /> $ch = curl_init($url);<br /> curl_setopt($ch, CURLOPT_HEADER, 0);<br /> curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br /> $output = curl_exec($ch);<br /> curl_close($ch);<br /> return $output;<br />}<br />get_shuxing('taobao',45098460735);
登录后复制