1. 淘寶客API接口獲取商品信息
<?php
/*配置參數*/
$appkey = 'xxx';//AppKey
$secretKey = 'xxx';//AppSecret
$url = "http://gw.api.taobao.com/router/rest";
$sessionKey = 'xxx';//授權時獲得的token
$keyworld = urlencode('連衣裙');//需要搜索的商品關鍵字
/*API參數*/
$params = array(
'method' =>'taobao.tbk.item.get',
'fields' =>'num_iid,title,pict_url,reserve_price,zk_final_price,user_type,provcity,item_url',
'q' =>$keyworld,
'cat' =>'',
'itemloc' =>'',
'sort' =>'tk_rate_des',
'is_tmall' =>'',
'is_overseas' =>'false',
'start_price' =>'',
'end_price' =>'',
'start_tk_rate' =>'',
'end_tk_rate' =>'',
'platform' =>'1',
'page_no' =>'2',
'page_size' =>'20'
);
/*生成簽名*/
ksort($params);
$str = '';
foreach ($params as $k =>$v) {
$str .= "$k$v";
}
$sig = strtoupper(md5($secretKey . $str . $secretKey));
$params['sign'] = $sig;
$params['app_key'] = $appkey;
$params['session'] = $sessionKey;
/*API請求*/
$url = $url . "?" . http_build_query($params);
$result = file_get_contents($url);
$ar = json_decode($result, true);
/*處理結果*/
foreach($ar['results']['n_tbk_item'] as $k=>$v){
echo $v['title'].'<br/>';
}
?>
2. 使用淘寶Web自動化工具獲取商品信息
<?php
use QL\QueryList;
require_once('./vendor/autoload.php');
//登錄淘寶
$login_url = 'https://login.taobao.com/member/login.jhtml';
$cookies_txt = './cookies.txt';
$ql_tmp = QueryList::getInstance()->get($login_url);
$cookies = $ql_tmp->getCookies();
$action_url = $ql_tmp->find('#J_LoginQuick a')->attr('href');
$login_site = false;
if( !$action_url ){
foreach($cookies as $item){
if($item['name'] == '_tb_token_' ){
$login_site = true;
}
}
}
if( !$login_site ){
$ql = QueryList::getInstance()
->get($login_url, '', array('timeout'=>30,
'headers' =>array(
'Referer'=>'http://www.taobao.com',
)
));
$postData['_csrf_token'] = $ql->find('input[name=_csrf_token]')->val();
$postData['TPL_username'] = ''; //賬號
$postData['TPL_password'] = ''; //密碼
$postData['TPL_checkcode'] = ''; //驗證碼
$qlql = QueryList::getInstance()
->post($action_url, $postData, array('headers' =>array(
'Referer'=>'https://login.taobao.com/member/login.jhtml',
))
);
$login_site = true;
file_put_contents($cookies_txt, json_encode($qlql->getCookies()));
}
//數據獲取
$url = 'https://s.taobao.com/search?q=%E8%BF%9E%E8%A1%A3%E8%A3%99&refpid=420460_1006&source=related_search&spm=a231k.7960580.0.0.5a43edbc6yIjpn';
$cookies = file_get_contents($cookies_txt);
$ql = QueryList::getInstance()
->get($url, '', array(
'timeout'=>30,
'user_agent' =>'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36',
'headers' =>array(
'cookie' =>$cookies
)
));
$data = $ql->find('.item-info-wrapper')->map(function($item){
return $item->find('.item-title')->text(). ' '.
$item->find('.item-location')->text(). ' ';
});
print_r($data->all());
?>
上一篇php 淘寶采集
下一篇aliyun php