当前位置: 移动技术网 > IT编程>开发语言>PHP > php爬取天猫和淘宝商品数据

php爬取天猫和淘宝商品数据

2018年03月09日  | 移动技术网IT编程  | 我要评论

bvt云购网,女人养生食谱,借贷宝头条

一、思路

最近做了一个网站用到了从网址爬取天猫和淘宝的商品信息,首先看了下手机端的网页发现用的react,不太了解没法搞,所以就考虑从pc入口爬取数据,但是当爬取url获取数据时并没有获取价格,库存等的信息,仔细研究了下发现是异步请求了另一个接口,但是接口要使用refer才能获取数据,于是就通过以下方式写了一个简单的爬虫,用于爬取商品预览图和商品的第一个分类的价格、库存等。

二、实现

代码如下:

function crawlurl($url){
import('phpquery.curl');
 $curl=new \curl();
 $result = $curl->read($url);
 $content = mb_convert_encoding( $result['content'], 'utf-8', 'utf-8,gbk,gb2312,big5' );
 $myres=array();
 if(strrpos($url,'taobao.com')!=false) {
  //匹配是否下架
  if(strpos($content,'此宝贝已下架')!==false){
   return false;
  }
  preg_match("|itemid   : '(.*)'|isu", $content, $match);
  $item_id=$match[1];
  preg_match("|sellerid   : '(.*)'|isu", $content, $match);
  $sellet_id=$match[1];
  preg_match("|<title>(.*)</title>|isu",$content,$match);
  $title=$match[1];
  //价格库存信息
  $ch = curl_init();
  curl_setopt ($ch, curlopt_url, 'https://detailskip.taobao.com/service/getdata/1/p1/item/detail/sib.htm?itemid='.$item_id.'&sellerid='.$sellet_id.'&modules=dynstock,qrcode,viewer,price,duty,xmppromotion,delivery,upp,activity,fqg,zjys,amountrestriction,couponactivity,soldquantity,originalprice,tradecontract&callback=onsibrequestsuccess');
  $opt[curlopt_header]=false;
  $opt[curlopt_connecttimeout]=15;
  $opt[curlopt_timeout]=300;
  $opt[curlopt_autoreferer]=true;
  $opt[curlopt_useragent]='mozilla/5.0 (windows nt 6.1) applewebkit/536.11 (khtml, like gecko) chrome/20.0.1132.47 safari/536.11';
  curl_setopt_array($ch,$opt);
  curl_setopt ($ch, curlopt_returntransfer, 1);
  curl_setopt ($ch,curlopt_referer,$url);
  curl_setopt($ch, curlopt_ssl_verifypeer, false);
  $out_put=curl_exec ($ch);
  curl_close ($ch);
  $res=str_replace('onsibrequestsuccess(',"",$out_put);
  $res=rtrim($res,');1');
  $result=json_decode($res,true);
  //查询出图片信息
  preg_match('|<ul id="j_ulthumb" class="tb-thumb tb-clearfix">(.*)</ul>|isu', $content, $match);
  preg_match_all('/<img data-src="(.*?)" \//', $match[1], $images);

  $myres['title']=str_replace('-淘宝网','',$title);

  $myres['price']=current($result['data']['originalprice']);

  $myres['act_price']=current($result['data']['promotion']['promodata']);

  $myres['stock']=$result['data']['dynstock']['stock'];

  $myres['banners']=$images[1];
 }else{
  //匹配是否下架
  if(strpos($content,'此宝贝已下架')!==false){
   return false;
  }
  $start=strpos($url,'&id=');
  $item_id=substr($url,$start+4,12);
  if(!is_numeric($item_id)){
   $start=strpos($url,'?id=');
   $end=strpos($url,'&spm');
   $item_id=substr($url,$start+4,$end-$start-4);
  }
  preg_match("|<title>(.*)</title>|isu",$content,$match);
  $title=$match[1];
  $myurl='https://mdskip.taobao.com/core/inititemdetail.htm?cachedtimestamp=1500562177777&querymemberright=true&cartenable=true&offlineshop=false&addresslevel=2&itemid='.$item_id.'&trybeforebuy=false&isareasell=false&tmallbuysupport=true&ispurchasemallpage=false&household=false&isforbidbuyitem=false&service3c=false&isregionlevel=false&showshopprom=false&isseckill=false&sellerpreview=false&isuseinventorycenter=false&isapparel=true&callback=setmdskip×tamp=1500562172109&isg=aiuldzfwmp/smgvurqsilu3ytet/zdis&isg2=ajk51jihrfqkzxminpp6dkyxskxt7iyskzstevtu9wdf4ll0o5y9ykdyethu';
  //价格库存信息
  $ch = curl_init();
  curl_setopt ($ch, curlopt_url, $myurl);
  $opt[curlopt_header]=false;
  $opt[curlopt_connecttimeout]=15;
  $opt[curlopt_timeout]=300;
  $opt[curlopt_autoreferer]=true;
  $opt[curlopt_useragent]='mozilla/5.0 (windows nt 6.1) applewebkit/536.11 (khtml, like gecko) chrome/20.0.1132.47 safari/536.11';
  curl_setopt_array($ch,$opt);
  curl_setopt ($ch, curlopt_returntransfer, 1);
  curl_setopt ($ch,curlopt_referer,$url);
  curl_setopt($ch, curlopt_ssl_verifypeer, false);
  $out_put=curl_exec ($ch);
  curl_close ($ch);
  $res = mb_convert_encoding( $out_put, 'utf-8', 'utf-8,gbk,gb2312,big5' );
  $res=str_replace('setmdskip',"",$res);
  $res=str_replace('(',"",$res);
  $res=str_replace(')',"",$res);
  $result=json_decode($res,true);
  $nowk="";
  $nowstore="";
  foreach($result['defaultmodel']['inventorydo']['skuquantity'] as $k=>$val){
   $nowk=$k;
   $nowstore=$val;
   break;
  }

  $myres['title']=str_replace('-tmall.com天猫','',$title);

  $myres['price']=$result['defaultmodel']['itempriceresultdo']['priceinfo'][$nowk]['price'];

  $myres['act_price']=isset($result['defaultmodel']['itempriceresultdo']['priceinfo'][$nowk]['suggestivepromotionlist'])?$result['defaultmodel']['itempriceresultdo']['priceinfo'][$nowk]['suggestivepromotionlist']:$result['defaultmodel']['itempriceresultdo']['priceinfo'][$nowk];

  $myres['stock']=$result['defaultmodel']['inventorydo']['totalquantity']?$result['defaultmodel']['inventorydo']['totalquantity']:$nowstore['quantity'];
  //查询出图片信息
  preg_match('|<ul id="j_ulthumb" class="tb-thumb tm-clear">(.*)</ul>|isu',$content, $match);
  preg_match_all('/<img src="(.*?)" \//',$match[1],$images);
  $myres['banners']=$images[1];
 }
 return $myres;
}

上述代码用到phpquery的库,但是其实没啥用,直接用curl就行,具体爬取的数据可以穿参查看结果,方法不区分淘宝和天猫链接,但是前提是必须是pc端链接,另外正则写的不规范,所以可以自己重写正则来匹配数据。

更多内容请参考专题《python爬取功能汇总》进行学习。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网