文章目录
- 1.目标、思路
- 2. 获取数据
- 3.解析数据
- 4.保存数据
1.目标、思路
目标: 本次爬虫爬取的目标是唯品会中口红分类的商品。(url
)
思路:打开网页后点击F12打开抓包工具,找到我们想要的数据包,并分析。
并且可以发现他是通过提交商品id参数来获取这些商品信息的,所以这不是个静态网页,在网页源代码中是找不到商品信息的。
那我们就获取所有的商品ID并传入到商品的数据包URL地址里面就可以获取商品信息了。
2. 获取数据
构建url,headers,params去请求数据。
import requests
headers = {'referer':'https://category.vip.com/','user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'}
url = 'https://mapi.vip.com/vips-mobile/rest/shopping/pc/search/product/rank'
params = {
'app_name': 'shop_pc',
'app_version': '4.0',
'warehouse': 'VIP_HZ',
'fdc_area_id': '104102101',
'client': 'pc',
'mobile_platform': '1',
'province_id': '104102',
'api_key': '70f71280d5d547b2a7bb370a529aeea1',
'user_id': '',
'mars_cid': '1616561542264_dbf891698c85094d527273d671f70e8f',
'wap_consumer': 'a',
'standby_id': 'nature',
'keyword': '口红',
'lv3CatIds': '',
'lv2CatIds': '',
'lv1CatIds': '',
'brandStoreSns': '',
'props': '',
'priceMin': '',
'priceMax': '',
'vipService': '',
'sort': '0',
'pageOffset': '0',
'channelId': '1',
'gPlatform': 'PC',
'batchSize': '120',
'_': '1616566409850'}response = requests.get(url = url ,params= params,headers=headers)print(response.json())
由下图可知获得了我们想要的pid数据。
3.解析数据
我们获取了商品的pid信息后就可以构建producid参数,并创建一个函数,这个函数的作用就是往里传pid数据,给我们返回商品信息,实现代码如下:
先构建pid:
products = response.json()['data']['products']
listt = []
for index in products:pid = index['pid']listt.append(pid)string_1 = ','.join(listt[0:50])
string_2 = ','.join(listt[50:100])
string_3 = ','.join(listt[100:])
再构建函数:
def get_info(pid):html_url = 'https://mapi.vip.com/vips-mobile/rest/shopping/pc/product/module/list/v2'params_1 = {'app_name': 'shop_pc','app_version': '4.0','warehouse': 'VIP_HZ','fdc_area_id': '104102101','client': 'pc','mobile_platform': '1','province_id': '104102','api_key': '70f71280d5d547b2a7bb370a529aeea1','user_id': '','mars_cid': '1616561542264_dbf891698c85094d527273d671f70e8f','wap_consumer': 'a','productIds': pid,'scene': 'search','standby_id': 'nature','extParams': '{"stdSizeVids":"","preheatTipsVer":"3","couponVer":"v2","exclusivePrice":"1","iconSpec":"2x","ic2label":1}','context': '','_': '1616566409853'}response_1 = requests.get(url = html_url ,params= params_1 ,headers =headers)pprint.pprint(response_1.json())
由下图可知获取了我们想要的数据 。
接下来完善函数,获取各个商品细则,代码如下:
def get_info(pid):html_url = 'https://mapi.vip.com/vips-mobile/rest/shopping/pc/product/module/list/v2'params_1 = {'app_name': 'shop_pc','app_version': '4.0','warehouse': 'VIP_HZ','fdc_area_id': '104102101','client': 'pc','mobile_platform': '1','province_id': '104102','api_key': '70f71280d5d547b2a7bb370a529aeea1','user_id': '','mars_cid': '1616561542264_dbf891698c85094d527273d671f70e8f','wap_consumer': 'a','productIds': pid,'scene': 'search','standby_id': 'nature','extParams': '{"stdSizeVids":"","preheatTipsVer":"3","couponVer":"v2","exclusivePrice":"1","iconSpec":"2x","ic2label":1}','context': '','_': '1616566409853'}response_1 = requests.get(url = html_url ,params= params_1 ,headers =headers)#pprint.pprint(response_1.json())products_list = response_1.json()['data']['products']for i in products_list:# 标题title = i['title']# 品牌show_name = i['brandShowName']# 原价marker_price = i['price']['marketPrice']# 折扣discount = i['price']['saleDiscount']# 现价sale_price = i['price']['salePrice']print(title,show_name,marker_price,discount,sale_price)
右下图可知,得到了我们想要的数据。
4.保存数据
导入csv模块,将数据写入csv文件中。
f = open('商品数据.csv',mode = 'a',encoding='utf-8',newline='')
csv_writer = csv.DictWriter(f,fieldnames=['标题','品牌','原价','折扣','售价'])
csv_writer.writeheader()
并在函数中添加如下代码以写入数据,
dit = {'标题': title,'品牌': show_name,'原价': marker_price,'折扣': discount,'售价': sale_price}csv_writer.writerow(dit)
右下图可知成功创建了csv文件并获取到了数据。