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

2023-11-01

第一步、打开唯品会网站  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 quote

def 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()

结果:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐

  • 关于hive中从hdfs上load数据到表中而HDFS上的数据却消失的若干问题

    原链接 https blog csdn net shuaikang666 article details 80357075 今天偶然间发现hive中一个我之前没有注意到的一个小细节 我怀疑你们之前也可能没有注意到 那就是当我们试图从HDFS
  • Adding New Functions to MySQL(User-Defined Function Interface UDF、Native Function)

    catalog 1 How to Add New Functions to MySQL 2 Features of the User Defined Function Interface 3 User Defined Function 4
  • postgres数据库相关使用说明

    默认的数据库和用户名是postgres 登录 psql U postgres d postgres ctrl c q 退出数据库交互模式 创建新用户 gwp createuser U postgres P d gwp 输入密码 mxq123
  • 路由器和交换机工作原理

    路由器工作原理 路由器 三层设备 同时基于二层设备工作 当数据包进到路由器时 首先查看的是二层报头 查看的是目标MAC 目标MAC分为三种 广播 组播 单播 广播地址 解封装到三层报头 组播地址 每一个组播地址均存在自己的MAC地址 基于目
  • 华为OD题目: 任务总执行时长

    package com darling boot order od od10 import com sun org apache bcel internal generic IF ACMPEQ import java util 任务总执行时
  • 几种I/O编程实践

    1 传统的BIO编程 网络编程的基本模型是Client Server模型 也就是两个进程间相互通信 其中 服务端提供位置信息 绑定的IP地址和监听端口 客户端提供连接操作向服务端监听的地址发起连接请求 通过三次握手建立连接 如果连接建立成功
  • Burpsuite在Firefox中无法抓取DVWA本地数据包解决方案+导入证书

    前言 这几天重装了系统 软件也大部分重新安装 在使用bp时 遇到了不能抓取dvwa数据包的情况 解决方案 猜想 可能是浏览器自动将127 0 0 1与localhost默认选择不使用代理服务 无法修改 反正我没找到 方案 将url栏中的12
  • java计算下一个整5分钟时间点

    需求 需要获取当前时间的下一个整点时间 如13 23 获取的下一个时间为 13 25 代码 获取下一个分钟值以0或者5结尾的时间点 单位 毫秒 return public static long getNextMillisEndWithMi
  • 机器数——源码、反码、补码

    机器数 源码 反码 补码 基本定义 1 机器数是将符号 数字化 的数 是数字在计算机中的二进制表示形式 表示一个机器数 应该考虑以下三个因素 1 机器数的范围 2 机器数的符号 3 机器数中小数点的位置 我们这里只讨论二进制整数在计算机中的
  • 【Java筑基】IO流基础之常见工具流和进程通信

    前 言 作者简介 半旧518 长跑型选手 立志坚持写10年博客 专注于java后端 专栏简介 深入 全面 系统的介绍java的基础知识 文章简介 本文将深入全面介绍IO流知识 建议收藏备用 创作不易 敬请三连哦 大厂真题 大厂面试真题大全
  • Python3 入门及基础语法

    文章目录 解释型语言 解释型语言优缺点 和编译性语言的区别 Python 简介 优点 缺点 和其他语言区别 Python 入门 Python 解释器安装 Python 继承开发环境安装 第一个 Python 程序 Python 基础 注释
  • MySql的时区(serverTimezone)引发的血案

    前言 mysql8 x的jdbc升级了 增加了时区 serverTimezone 属性 并且不允许为空 血案现场 配置jdbc的URL jdbc mysql IP PORT DB characterEncoding utf8 useSSL
  • Unity-人物移动

    Unity 人物移动 人物模型 参考以下视频 如何在Unity中导入pmx格式的MMD模型 哔哩哔哩 bilibili 用的是原神模型 这里要注意导入后把人物模型的Rig换为Humanoid 人物动作 使用的Unity Chan Model
  • iOS设备分辨率和icon尺寸

    经常需要告诉设计关于iPhone的分辨和icon的需要的尺寸 有时候自己也忘记了 都是从文档 Human Interface Guidelines 中取的 mark一下 icon相关 Device or context Icon size
  • Ubuntu 22 Server安装docker

    系统版本 Ubuntu 22 Server 按照如下文章进行了安装 Ubuntu 22 安装Docker环境
  • 升级go1.18版本json-iterator coredump问题

    unexpected fault address 0x0 fatal error fault signal SIGSEGV segmentation violation code 0x80 addr 0x0 pc 0x46639f goro
  • sqlserver千万数据查询分页

    sqlserver千万数据查询分页 前言废话 sqlserver 作业调用 mysql 前言废话 人生开始感受到无力 我不是没心没肺的人 可是我心里真的不舒服 sqlserver 新建一个表 if OBJECT ID test is not
  • tensorflow SSD实战:基于深度学习的多目标识别

    SSD SSD Single Shot MultiBox Detector 是采用单个深度神经网络模型实现目标检测和识别的方法 如图2所示 该方法是综合了Faster R CNN的anchor box和YOLO单个神经网络检测思路 YOLO
  • DataX-一款稳定高效的数据同步工具-从安装、启动、配置、使用总结,看这篇让你一步到位

    前言 大数据部门现阶段ETL按同步方式分为两种 实时同步 DTS CloudCanal 离线同步 dataworks DI节点 但CloudCanal在使用中出现了部分问题 归纳总结后主要为以下几点 部分使用场景获取不到binlog点位 停
  • python爬虫实战-如何批量爬取唯品会商品信息>>>

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