數據分析離不開數據的支持,為了分析唯品會,特地采集唯品會數據。
采集入口為手機端,在火狐瀏覽器下ctrl+shift+M進入手機模式,並點擊觸屏模式,進入唯品會網站m.vip.com,刷新網頁。
點擊右上角的搜索:
點擊品牌:
這時候打開火狐的firebug,隨便進入一個店鋪,這時候系統會向唯品會發送一個post,可以在firebug里面找到這個post如下圖:
點開+號,選擇post:
可以看到框起來的部分就是發送的post。由於唯品會這里屬於異步瀑布流加載,我們下拉頁面,觀察下一個post,看一下翻頁功能是哪一個。最終得到的頁面如下:
可以看到np變成了3,其實老爬蟲都會知道,np一般是代表翻頁的,那么我們的程序可以寫個翻頁的代碼:
1 # !/usr/bin/python3.4 2 # -*- coding: utf-8 -*- 3 # 抓取的頁數 4 try: 5 np = int(input("請輸入抓取的頁數:(默認10頁):")) 6 if np == 0: 7 np = 10 8 except: 9 np = 10
接下來看到在頁面首部有如下選項:
一個一個點擊,比如有貨商品,觀察發送的post:
可以看到這個:
filter_stock:1
所以寫下:
1 # !/usr/bin/python3.4 2 # -*- coding: utf-8 -*- 3 # 是否包含售罄商品 4 try: 5 filter_stock = int(input("看全部商品請按0,只看有貨請按1(默認看全部商品):")) 6 if filter_stock == 1: 7 filter_stock = 1 8 else: 9 filter_stock = 0 10 except: 11 filter_stock = 0
其他的價格排序和折扣排序也是這樣子觀察,最后得到:
1 # !/usr/bin/python3.4 2 # -*- coding: utf-8 -*- 3 # 價格的排序方式 4 sort = input("綜合排序請按0,價格升序請按1,降序請按2,折扣升序請按3,降序請按4(默認綜合排序):") 5 try: 6 if sort == "1": 7 sort = "1" 8 elif sort == "2": 9 sort = "2" 10 elif sort == "3": 11 sort = "3" 12 elif sort == "4": 13 sort = "4" 14 else: 15 sort = "0" 16 except: 17 sort = "0"
翻頁篩選搞定,下面看不同店鋪之間哪里不一樣,退到這個頁面:
以此點開不同的專場,可以發現代表專場的post是這個:
brand-838319-0-0-0-1-0-1-20.html
其中838319是專場的id,在地址欄可以看得到的
而不同的商品種類的id也是不一樣:
"id":1472638528843,
這個id暫時沒有發現有什么規律,不過按照道理來說唯品會的商品類目也就那么幾十種,做個窮舉就行了。
最終的postdata構造完畢:
1 # !/usr/bin/python3.4 2 # -*- coding: utf-8 -*- 3 postdata = {"method": "getGoodsList", 4 "params": { 5 "page": brandid, 6 "np": numbers, 7 "ep": 20, 8 "cat_id": "0", 9 "sort": sort, 10 "filter_stock": filter_stock, 11 "filter_size": "0", 12 "show": 0, 13 "query": ""}, 14 "id": 1472634230417, 15 "jsonrpc": "2.0"}
構造網址:
url = "http://m.vip.com/server.html"
上面為思路,搞定,下面是代碼。
-----------------------------我是快樂的分割線--------------------------------
如果看過:
python3抓取異步百度瀑布流動態圖片(二)get、json下載代碼講解
你就會知道這個也是要用上面解析頭:
1 def postget(url, postdata): 2 # 制作頭部 3 header = { 4 'User-Agent': 'Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5', 5 'Referer': 'http://m.vip.com/', 6 'Host': 'm.vip.com', 7 'Accept': 'application/json', 8 'Accept-Encoding': 'gzip, deflate', 9 'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3', 10 'Connection': 'keep-alive', 11 'Content-Type': 'application/json' 12 } 13 # post參數 14 res = requests.post(url=url, headers=header, json=postdata) 15 # ('UTF-8')('unicode_escape')('gbk','ignore') 16 resdata = res.content 17 return resdata
利用這個解析頭將解析出來的東西保存為json:
1 # !/usr/bin/python3.4 2 # -*- coding: utf-8 -*- 3 # 解析得到json 4 data = postget(url, postdata) 5 # 將抓到的儲存到json里面 6 savepath = r"../json/" 7 file = open(savepath + str(numbers) + '.json', 'wb') 8 file.write(data) 9 file.close()
讀取json,並采集自己需要的東西:
1 # !/usr/bin/python3.4 2 # -*- coding: utf-8 -*- 3 # 讀取json 4 checkpath = r"../json/" 5 files = listfiles(checkpath, '.json') 6 for filename in files: 7 try: 8 doc = open(filename, 'rb') 9 doccontent = doccontent = doc.read().decode('unicode_escape', 'ignore') 10 doccontent = doccontent.replace(' ', '').replace('\n', '').replace('<spanclass="salebg2">0.8<\/span>折起', '') 11 except Exception as err: 12 print(err) 13 pass 14 15 jsondata = json.loads(doccontent) 16 # 里面的格式為 17 # [{"method":"getClassifyList","result":{"products":[ 18 for item in jsondata: 19 onefile = item['result'] 20 21 towfile = onefile["products"] 22 for item in towfile: 23 # 共有的項目是專場id、專場名字 24 itemlist = [item["brand_id"], item["brand_name"]] 25 # 貨號 26 itemlist.append(item['merchandise_sn']) 27 # 商品id 28 itemlist.append(item['product_id']) 29 # 商品名字 30 itemlist.append(item['product_name']) 31 # 圖片網址 32 itemlist.append(item['small_image']) 33 # 賣價 34 itemlist.append(item['vipshop_price']) 35 # 折扣 36 itemlist.append(item['discount']) 37 # 吊牌價 38 itemlist.append(item['market_price']) 39 # sku_id 40 itemlist.append(item['sku_id']) 41 # spu_id 42 itemlist.append(item['v_spu_id'])
將其寫入excel:
1 # !/usr/bin/python3.4 2 # -*- coding: utf-8 -*- 3 4 # 創建一個Excel文件 5 workbook = xlsxwriter.Workbook('../excel/' + today + '.xlsx') 6 # 創建一個工作表 7 worksheet = workbook.add_worksheet() 8 # 記錄第一行 9 first = ['商品種類id', '品牌名字', '專場id', '專場名字', '商品id', '商品名字', '圖片網址', '賣價', '折扣', '吊牌價', 'sku_id', '優惠'] 10 # 寫入excel的行數 11 num = 1 12 13 # 寫入第一行 14 for m in range(0, len(first)): 15 worksheet.write(0, m, first[m]) 16 try: 17 worksheet.write(num, m, itemlist[m]) 18 except Exception as err: 19 worksheet.write(num, m, "") 20 print(err) 21 num = num + 1
順便下載點圖片:
1 # !/usr/bin/python3.4 2 # -*- coding: utf-8 -*- 3 4 pic = urllib.request.urlopen(item['small_image']) 5 picno = time.strftime('%H%M%S', time.localtime()) 6 filenamep = "../image/" + picno + '-' + validateTitle(item["merchandise_sn"] + '-' + item['product_name']) 7 filenamepp = filenamep + '.jpg' 8 filess = open(filenamepp, 'wb') 9 filess.write(pic.read()) 10 filess.close() 11 print("下載圖片" + str(item['small_image'])) 12 time.sleep(1)
全部搞定,效果圖: