第一步、打开唯品会网站  https://www.vip.com。然后随意搜索一种商品,比如"键盘",搜索之后下拉发现页面URL没有发生改变,但是商品信息在不断加载,那么这就是动态Ajax技术,遇到这种情况,第一反应就是找接口。

第二步、打开开发者工具,鼠标右键,点击检查,切换到Network选项卡,然后刷新唯品会页面,进行抓包,然后查看每个包的pirview,发现商品信息在‘ v2?callback=getMerchandise’中,我们来看一下URL,不看不要紧,一看吓一跳-_-,这URL也太长了,研究一下参数,发现主要是每件商品都有自己的pid,那么接下来,只要我们找到商品的pid就可以抓取数据了。

继续在Network抓到的包中查看每个包的priview,最终在‘rank?callback=getMerchandis’中找到了商品的pid。接下来就好办了,先切换到headers,查看url参数,在唯品会页面翻页,发现改变的只有pageOffset,每次翻页pageOffset增加120,那么每页的商品有120件,而且如果换一件商品进行搜索,只有keyword改变,了解了这一点,我们就可以实现搜索商品关键词然后得到对应的商品信息,并且可以进行翻页。

第三步、获取商品的pid。 访问‘rank?callback=getMerchandis’中的URL,参数keyword,和pageOffset可以进行修改,以达到自己想要的信息,然后请求HTML页面,记得加上请求头。在‘rank?callback=getMerchandis’包中的 priview中可以得知,该页面返回的是json数据,而且是不合法的json那么就要将不合法的json,那么就要将不合法的json转换成字典,方便取出pid,直接上代码。

​
​
keyword = input('请输入想要查询的商品关键词>>>')
pagenum = int(input('请输入页数,每页120个商品>>>'))
for i in range(0,pagenum):url = 'https://mapi.vip.com/vips-mobile/rest/shopping/pc/search/product/rank?callback=getMerchandiseIds&app_name=shop_pc&app_version=4.0&warehouse=VIP_HZ&fdc_area_id=104101108&client=pc&mobile_platform=1&province_id=104101&api_key=70f71280d5d547b2a7bb370a529aeea1&user_id=&mars_cid=1628070214309_e7fbca2c43dda020cc7734c00466d49c&wap_consumer=a&standby_id=nature&keyword{}&lv3CatIds=&lv2CatIds=&lv1CatIds=&brandStoreSns=&props=&priceMin=&priceMax=&vipService=&sort=0&pageOffset{}&channelId=1&gPlatform=PC&batchSize=120&_=1628070503449'.format(quote(keyword),120*i)headers = {'referer': 'https://category.vip.com/','user-agent': 'Mozilla/5.0'
}html = requests.get(url,headers = headers)# print(html.text)start = html.text.find('{"code"')json_data = json.loads(html.text[start:-1])['data']['products']# print(json_data)for data in json_data:pid = data['pid']# print(pid)​​

第四步、有了商品pid再回到‘ v2?callback=getMerchandise’中,将商品pid放到URL里面然后再求情就可以得到商品的json数据,再次转换成字典格式,然后想要什么信息,直接从字典里取出来就行。

for data in json_data:pid = data['pid']# print(pid)stuff_url = 'https://mapi.vip.com/vips-mobile/rest/shopping/pc/product/module/list/v2?callback=getMerchandiseDroplets1&app_name=shop_pc&app_version=4.0&warcallback=getMerchandiseDroplets1&app_name=shop_pc&app_version=4.0&warehouse=VIP_HZ&fdc_area_id=104101108&client=pc&mobile_platform=1&province_id=104101&api_key=70f71280d5d547b2a7bb370a529aeea1&user_id=&mars_cid=1628070214309_e7fbca2c43dda020cc7734c00466d49c&wap_consumer=a&productIds={}&scene=search&standby_id=nature&extParams=%7B%22stdSizeVids%22%3A%22%22%2C%22preheatTipsVer%22%3A%223%22%2C%22couponVer%22%3A%22v2%22%2C%22exclusivePrice%22%3A%221%22%2C%22iconSpec%22%3A%222x%22%2C%22ic2label%22%3A1%7D&context=&_=1628071156110'.format(pid)stuff_html = requests.get(stuff_url,headers = headers)# print(stuff_html.text)start = stuff_html.text.find('{"code"')end = stuff_html.text.find('"}}')+len('"}}')stuff_json_data = json.loads(stuff_html.text[start:end])['data']['products']# print(stuff_json_data)for stuff_data in stuff_json_data:title = stuff_data['title']price = stuff_data['price']['salePrice']imgurl = stuff_data['squareImage']print('名称:{},价格:{}元'.format(title,price))print(imgurl)

第五步、将数据保存到本地txt文本中

with open('{}商品信息.txt'.format(keyword),'a',encoding='utf8')as f:f.write('商品名称:{},价格:{}元'.format(title,price)+'\n')f.write('商品图片链接:{}'.format(imgurl)+'\n')

===最后把源代码奉上,原创作品,记得点赞哦,点赞的人会变帅,会变得更有钱!!!

import requests
import json
from urllib.parse import quotedef get_weipin_info():keyword = input('请输入想要查询的商品关键词>>>')pagenum = int(input('请输入页数,每页120个商品>>>'))for i in range(0,pagenum):url = 'https://mapi.vip.com/vips-mobile/rest/shopping/pc/search/product/rank?callback=getMerchandiseIds&app_name=shop_pc&app_version=4.0&warehouse=VIP_HZ&fdc_area_id=104101108&client=pc&mobile_platform=1&province_id=104101&api_key=70f71280d5d547b2a7bb370a529aeea1&user_id=&mars_cid=1628070214309_e7fbca2c43dda020cc7734c00466d49c&wap_consumer=a&standby_id=nature&keyword={}&lv3CatIds=&lv2CatIds=&lv1CatIds=&brandStoreSns=&props=&priceMin=&priceMax=&vipService=&sort=0&pageOffset={}&channelId=1&gPlatform=PC&batchSize=120&_=1628070503449'.format(quote(keyword),120*i)headers = {'referer': 'https://category.vip.com/','user-agent': 'Mozilla/5.0'}html = requests.get(url,headers = headers)# print(html.text)start = html.text.find('{"code"')json_data = json.loads(html.text[start:-1])['data']['products']# print(json_data)for data in json_data:pid = data['pid']# print(pid)stuff_url = 'https://mapi.vip.com/vips-mobile/rest/shopping/pc/product/module/list/v2?callback=getMerchandiseDroplets1&app_name=shop_pc&app_version=4.0&warcallback=getMerchandiseDroplets1&app_name=shop_pc&app_version=4.0&warehouse=VIP_HZ&fdc_area_id=104101108&client=pc&mobile_platform=1&province_id=104101&api_key=70f71280d5d547b2a7bb370a529aeea1&user_id=&mars_cid=1628070214309_e7fbca2c43dda020cc7734c00466d49c&wap_consumer=a&productIds={}&scene=search&standby_id=nature&extParams=%7B%22stdSizeVids%22%3A%22%22%2C%22preheatTipsVer%22%3A%223%22%2C%22couponVer%22%3A%22v2%22%2C%22exclusivePrice%22%3A%221%22%2C%22iconSpec%22%3A%222x%22%2C%22ic2label%22%3A1%7D&context=&_=1628071156110'.format(pid)stuff_html = requests.get(stuff_url,headers = headers)# print(stuff_html.text)start = stuff_html.text.find('{"code"')end = stuff_html.text.find('"}}')+len('"}}')stuff_json_data = json.loads(stuff_html.text[start:end])['data']['products']# print(stuff_json_data)for stuff_data in stuff_json_data:title = stuff_data['title']price = stuff_data['price']['salePrice']imgurl = stuff_data['squareImage']print('名称:{},价格:{}元'.format(title,price))print(imgurl)with open('{}商品信息.txt'.format(keyword),'a',encoding='utf8')as f:f.write('商品名称:{},价格:{}元'.format(title,price)+'\n')f.write('商品图片链接:{}'.format(imgurl)+'\n')print('{}商品信息爬取完成'.format(keyword))if __name__ == '__main__':get_weipin_info()

结果:

python爬虫实战-如何批量爬取唯品会商品信息>>>相关推荐

  1. 【python爬虫实战】批量爬取站长之家的图片

    概述: 站长之家的图片爬取 使用BeautifulSoup解析html 通过浏览器的形式来爬取,爬取成功后以二进制保存,保存的时候根据每一页按页存放每一页的图片 第一页:http://sc.china ...

  2. 转 Python爬虫实战一之爬取糗事百科段子

    静觅 » Python爬虫实战一之爬取糗事百科段子 首先,糗事百科大家都听说过吧?糗友们发的搞笑的段子一抓一大把,这次我们尝试一下用爬虫把他们抓取下来. 友情提示 糗事百科在前一段时间进行了改版,导致 ...

  3. 《python爬虫实战》:爬取贴吧上的帖子

    <python爬虫实战>:爬取贴吧上的帖子 经过前面两篇例子的练习,自己也对爬虫有了一定的经验. 由于目前还没有利用BeautifulSoup库,因此关于爬虫的难点还是正则表达式的书写. ...

  4. python爬虫实战之多线程爬取前程无忧简历

    python爬虫实战之多线程爬取前程无忧简历 import requests import re import threading import time from queue import Queu ...

  5. Python爬虫实战一之爬取糗事百科段子

    点我进入原文 另外, 中间遇到两个问题: 1. ascii codec can't decode byte 0xe8 in position 0:ordinal not in range(128) 解 ...

  6. python爬虫实战之异步爬取数据

    python爬虫实战之异步爬取数据 文章目录 前言 一.需求 二.使用步骤 1.思路 2.引入库 3.代码如下 总结 前言 python中异步编程的主要三种方法:回调函数.生成器函数.线程大法. 以进 ...

  7. Python爬虫实战练习:爬取微信公众号文章

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:徐洲更 为了实现该爬虫我们需要用到如下工具 Chrome浏览器 Py ...

  8. Python爬虫实战Pro | (1) 爬取猫眼电影Top100榜单

    在本篇博客中,我们将使用requests+正则表达式来爬取猫眼电影官网的TOP100电影榜单,获取每部电影的序号,片名,主演,上映日期,评分和封面等内容. 之前在Python爬虫实战(1)中我们曾爬取 ...

  9. python爬虫实例方法(批量爬取网页信息基础代码)

    文章目录 前言 一.爬虫实例 0.爬取深圳租房信息 1.爬取深圳算法岗位信息 2.爬取猫图片(基于 selenium库 模拟人自动点击) 3.爬取小说纳兰无敌并生成词云 二.用到的库 1.正则表达式 ...

最新文章

  1. s3c2410上搭建QT/Embedded4.8.5开发环境(四)--安装intel-x86 X11平台qt库qt-everywhere-opensource-src-4.8.5...
  2. 使用四种框架分别实现百万websocket常连接的服务器--转
  3. 简述3032路pcm帧的结构_高级数据链路控制协议-HDLC
  4. Jeesite信息化快速开发平台
  5. 硬件:笔记本电脑7大分类总结,看完你就明白了!
  6. 2020蓝桥杯省赛---java---B---6(成绩分析)
  7. 手势UIGestureRecognizer
  8. 2017美赛C题论文学习笔记
  9. 系统集成项目管理工程师必考公式
  10. 关于电的计算机公式,电能计算-电能的计算公式-电工基础 - 电工屋
  11. GPS定位准确度CEP、RMS
  12. GAE—图自编码器/Graph RNN/Graph RL
  13. 【迁移学习】STL(Stratified Transfer Learning)小结
  14. 那些年啊,那些事——一个程序员的奋斗史 ——17
  15. http协议_代理服务(proxy)
  16. 微信小程序选项卡swiper默认高度150px(让高度实现自适应)怎么解决?
  17. 离散集合运算c语言程序,离散数学集合运算c语言.doc
  18. morph 原理实现
  19. 小米智能家居技术分析
  20. 漫画 | 最让程序员抓狂的7件事

热门文章

  1. 11.绘制统计图形——误差棒图
  2. Linux配置Samba实现局域网共享文件夹
  3. 咖啡产品介绍PPT模板
  4. 母牛2年生小牛 5年后并死去的算法
  5. win7笔记本外接显示器html,笔记本连显示器的步骤_笔记本如何外接显示器-win7之家...
  6. HDMI 连接笔记本与显示器
  7. csgo社区自建服务器,CSGO官方社区服黄页测试版上线
  8. 机器学习中的数学——常用概率分布(十一):狄利克雷分布(Dirichlet分布)
  9. Task 编程中的异常处理
  10. 海外权威媒体好评连连,一加5T中国11月28号发布