為什麼想起來做這個功能?是因為前段時間在做一個淘客網站的時候,想到是否能抓取到淘寶商品的買家秀呢?經過一番折騰發現,淘寶商品用戶評價信息是通過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>"; }*/ ?>
有什麼解析jsonp格式的好方法呢?求大神~~~