說起來做這個功能還真是一時好奇。前段時間在做一個淘客網站的時候,想到是否能抓取到淘寶商品的買家秀呢?經過一番折騰發現,淘寶商品用戶評價信息是通過Ajax來調取的,通過嗅探網址發現,評論數據的請求接口是:
https://rate.tmall.com/list_detail_rate.htm?itemId=524394294771&spuId=341564036&sellerId=100414600&order=3¤tPage=1&append=0&content=1&tagId=&posi=&picture=1&callback=jsonp2339
其實上面很多參數也很容易理解,itemId是商品的ID,currentPage是當前頁,picture為1時顯示有圖的評價,既然是抓取買家秀,那麼picture參數一定要為1了。
如果你直接去訪問上面的接口時,會得到如下圖所示的請求結果:
看到請求結果是jsonp格式我就蛋碎了,我不知道如何去解析,但是換種思路,直接用PHP的正則去解析也未嘗不可嘛,通過嘗試,已經正確的能夠解析到評論內容和買家秀的圖片內容,如圖:
效果不錯,代碼實現了評論內容的抓取、買家秀圖片的抓取,代碼奉上:
<?php $url = "https://rate.tmall.com/list_detail_rate.htm?itemId=524394294771&spuId=341564036&sellerId=100414600&order=3¤tPage=1&append=0&content=1&tagId=&posi=&picture=1&callback=jsonp2339"; $ch2 = curl_init(); curl_setopt($ch2, CURLOPT_URL, $url); curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch2, CURLOPT_RETURNTRANSFER, TRUE); $texts = curl_exec($ch2); curl_close($ch2); //echo $texts; $pattern = '/"pics"(.+?)","reply"/is'; preg_match_all($pattern, $texts, $match); for($i=0;$i<count($match[0]);$i++){ $pattern2 = '/"rateContent":"(.+?)."reply"/is'; preg_match($pattern2, $match[0][$i], $matchcomments_only); echo "<p>".str_replace('","rateDate":"',' ',str_replace('","reply"','',str_replace('"rateContent":"','',$matchcomments_only[0])))."</p>"; $pattern3 = '/img.alicdn(.+?).jpg/is'; preg_match($pattern3, $match[0][$i], $matchpic_only); echo '<img src="http://'.$matchpic_only[0].'" width=120>'; } /*匹配一張圖片 $pattern = '/"pics"(.+?)","position"/is'; preg_match_all($pattern, $texts, $matchpic); for($i=0;$i<count($matchpic[0]);$i++){ $pattern3 = '/img.alicdn(.+?).jpg/is'; preg_match($pattern3, $matchpic[0][$i], $matchpic_only); echo "<p>".$matchpic_only[0]."</p>"; }*/ /*匹配所有圖片 $pattern = '/"pics"(.+?)","position"/is'; preg_match_all($pattern, $texts, $matchpic); for($i=0;$i<count($matchpic[0]);$i++){ $pics_str=str_replace('"pics":["//','',str_replace('"],"picsSmall":"","position"','',$matchpic[0][$i])); $arr = explode('","//',$pics_str); echo "<p>"; foreach($arr as $newstr){ echo '<img src=http://'.$newstr.' width=100 >'; } echo "</p>"; }*/ ?>
下面給大家介紹PHP抓取淘寶搜索商品列表實例
<?php header("Content-Type:text/html;charset=gbk"); include "Snoopy.class.php"; $snoopy = new Snoopy; $snoopy->fetch("http://s.taobao.com/search?spm=a230r.1.8.7.2NN4M7&q=%C7%EF%B6%AC%B4%F3%D2%C2&source=tbsy&refpid=420461_1006&discount_index=1&newpre=null&p4p_str=fp_midtop%3D0%26firstpage_pushleft%3D0&style=list&s=0#J_Filter"); $html=$snoopy->results; //說明:抓取網頁使用的是 Snoopyphp 框架 也可以直接使用file_get_contents函數獲取即可, //var_dump($html); preg_match_all('/<h3 class="summary">.*</a>/',$html,$result); //preg_match_all('/<divsid="([a-z0-9_]+)">([^<>]+)</div>/',$html,$result); var_dump($result); echo "<br>"; /* //循環讀取數組 foreach($result as $key1){ foreach($key1 as $key => $val){ echo $key.$val; } } */
以上所述是小編給大家介紹的PHP抓取淘寶商品的用戶曬單評論+圖片+搜索商品列表實例,希望對大家有所幫助!