爬取朴朴某个商品信息

准备工作

  • 一、软件安装

    1. PC端下载:

      Python,PyCharmPyCharm专业版破解Fiddler哪个不会点击哪个看教程,Fidder不会的可以使用移动端的软件。二者择其一即可。

    2. 移动端下载:

      小蓝鸟密码:e944ae,虚拟空间,移动端的软件傻瓜式安装操作,虚拟空间里放入需要的抓取的软件。小蓝鸟设置指定虚拟空间,比较快捷。

  • 二、环境搭建与需求分析

    1. 选取语言与工具及理由:

      Python:在大家目前学习的c,java,python中个人认为python是功能行强,方面广,最方便上手的语言,基本语法会基本就没问题了。

      PyCharm:纯粹是因为我个人用的习惯,导入包时候没有的会自己下载。

      小蓝鸟:是在移动端比较方便的抓包工具,可以指定抓包,小窗口操作。

    2. 实现目标效果与需求分析:

      • 我们的目标是能够爬取商品的名字,规格,当前价格,市场价格,详细信息等信息形成如下图的文字输出

      1.所以我们至少要实现能得到商品的名字,规格,当前价格,市场价格,详细信息

      2.能够得到实时价格

      3.考虑到商品下架,不存在的输出提示

功能实现

  • 一、抓包分析

    1. 抓取需要的链接:

      我是使用小蓝鸟抓包,大家可以想想页面的参数不可能不变的,是实时更新的。他其实就是根据不同的参数实现回传数据的不同。大家应该能发现我们需要信息的返回链接是https://j1.pupuapi.com/client/product/storeproduct/detail/7c1208da-907a-4391-9901-35a60096a3f9/b3fc4708-3f1d-412a-8d18-efc4b6951fa7我们经过多次实验可以看出前面大家都是一样的,最后的7c1208da-907a-4391-9901-35a60096a3f9/b3fc4708-3f1d-412a-8d18-efc4b6951fa7是商品id,通过修改商品id可以实现得到不同商品的信息。

    2. 商品id正确时信息分析:

      通过链接得到上图显示信息,可以清晰的看到商品的各种信息。
      name:商品名
      spec:商品规格
      price:商品当前价格
      market_price:商品市场价格
      share_content:商品详细信息

    3. 商品id错误时信息分析

      可以看到提示已经下架。

  • 二、代码实现

    1. 得到链接反馈

      大家可以看看视频学习
      大家抓包时候会发现前面有表明get或者post方法,我们这次是get,所以使用requests.get(url, headers="User-Agent中的内容")得到信息反馈。User-Agent按F12然后查看头,在最下面的就是,这是标识你的访问设备信息。

    2. 提取自己需要的信息

      # 得到商品名字
      name = re.findall(r'name":"(.*?)",', res.text)[0]
      # 得到商品规格
      spec = re.findall(r'spec":"(.*?)",', res.text)[0]
      # 得到商品当前价格
      price = re.findall(r'price":(.*?),', res.text)[0]
      price = str(int(price) / 100)
      # 得到商品市场价格
      market_price = re.findall(r'market_price":(.*?),', res.text)[0]
      market_price = str(int(market_price) / 100)
      # 得到商品市场详细信息
      share_content = re.findall(r'share_content":"(.*?)",', res.text)[0]
    
    1. 最终实现代码
点击查看代码
      from time import strftime, sleep

      import requests
      import re


      def pupuMessage(productId):
          try:
              url = "https://j1.pupuapi.com/client/product/storeproduct/detail/" + productId
              head = {
                  # headerUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36
                  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36'
              }

              res = requests.get(url, headers=head)
              # res.encoding="utf-8"
              # 得到商品名字
              name = re.findall(r'name":"(.*?)",', res.text)[0]
              # 得到商品规格
              spec = re.findall(r'spec":"(.*?)",', res.text)[0]
              # 得到商品当前价格
              price = re.findall(r'price":(.*?),', res.text)[0]
              price = str(int(price) / 100)

              # 得到商品市场价格
              market_price = re.findall(r'market_price":(.*?),', res.text)[0]
              market_price = str(int(market_price) / 100)

              # 得到商品市场详细信息
              share_content = re.findall(r'share_content":"(.*?)",', res.text)[0]

              print("--------------" + name + "----------")
              print("规格:" + spec)
              print("价格:" + price)
              print("原价/折扣价:" + market_price + "/" + price)
              print("详细内容:" + share_content)
              print("\n\n--------------" + name + "的价格波动----------")
          except:
              url = "https://j1.pupuapi.com/client/product/storeproduct/detail/" + productId

              head = {
                  # headerUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36
                  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36'
              }

              res = requests.get(url, headers=head)
              errmsg = re.findall(r'errmsg":"(.*?)"', res.text)[0]
              print(errmsg)


      def now_price(productId):

          url = "https://j1.pupuapi.com/client/product/storeproduct/detail/" + productId

          head = {
              # headerUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36
              'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36'
          }
          res = requests.get(url, headers=head)
          price = re.findall(r'price":(.*?),', res.text)[0]
          price = str(int(price) / 100)
          nowTimeAndPrint = strftime('%Y' + '-' + '%m' + '-' + '%d' + ' %H:%M,价格为' + price)
          print(nowTimeAndPrint)


      if __name__ == '__main__':
          productId = "7c1208da-907a-4391-9901-35a60096a3f9/b0ae0105-c979-4006-8582-1acf84b48fe0"
          pupuMessage(productId)
          try:
              while (1):
                  now_price(productId)
                  sleep(3)

          except:
              print("进程结束")

  • 三、后续完善
    • 商品价格检测时间太短,时间跨度大一点。
    • 商品更新太快,商品下架很快
posted @ 2022-03-13 11:33  冥天肝  阅读(592)  评论(1编辑  收藏  举报