• 爬虫面试案例系列01


    爬虫面试案例系列01

     

    ### 需求:爬取https://m.vmall.com/help/hnrstoreaddr.htm荣耀线下门店中的门店详情信息。页面显示如下:

    - 首页显示

    - 详情页显示

     ### 基于抓包工具分析如下:

     ### 查看定位到数据包的请求头信息:

    - 请求的url和请求方式如下:

    - 请求携带的请求参数如下:

       - 注意:请求参数为字典格式并非常规的键值对,所以在代码实现中需要使用dumps将字典转成json串作为请求参数

    ### 代码实现:爬取到首页对应的门店信息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    import requests
    import json
    url = 'https://openapi.vmall.com/mcp/offlineshop/getShopList'
    data = {
        "portal":"2",
        "lang":"zh-CN",
        "country":"CN",
        "brand":"1",
        "province":"河北省",
        "city":"邯郸市",
        "pageNo":"2",
        "pageSize":"20"}
     
    #必须使用dumps操作
    json_data = requests.post(url,data=json.dumps(data)).json()

     - 请求到的数据为:

    - 请求到的数据分析:

      - 数据为门店相关数据,其中每一个门店有其对应的一个id值,我们需要将id值解析出来,在后面请求详情页会使用到

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    import requests
    import json
    url = 'https://openapi.vmall.com/mcp/offlineshop/getShopList'
    data = {
        "portal":"2",
        "lang":"zh-CN",
        "country":"CN",
        "brand":"1",
        "province":"河北省",
        "city":"邯郸市",
        "pageNo":"2",
        "pageSize":"20"}
    #从中解析出id
    json_data = requests.post(url,data=json.dumps(data)).json()
    for dic in json_data['shopInfos']:
        _id = dic['id'] #解析出门店的id值

    ### 请求每一个门店详情页的数据

    - 请求的url和请求方式:

     - 请求参数:

     发现只有shopId为动态变化的请求参数其他都是固定不变的,然后该shopId就是上一步我们解析出来的门店id,则基于门店id作为请求详情页的请求参数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    import requests
    import json
    url = 'https://openapi.vmall.com/mcp/offlineshop/getShopList'
    data = {
        "portal":"2",
        "lang":"zh-CN",
        "country":"CN",
        "brand":"1",
        "province":"河北省",
        "city":"邯郸市",
        "pageNo":"2",
        "pageSize":"20"}
    #从中解析出id
    json_data = requests.post(url,data=json.dumps(data)).json()
    for dic in json_data['shopInfos']:
        _id = dic['id']
        #拼接详情页的url
        detail_url = 'https://openapi.vmall.com/mcp/offlineshop/getShopById?portal=2&version=10&country=CN&shopId={}&lang=zh-CN'.format(_id)
        finally_data = requests.get(url=detail_url).json()
        print(finally_data)#每一页详情页url的数据
        

      

     
     
  • 相关阅读:
    node.js 建立live-server
    Django 反向解析
    Boost智能指针——weak_ptr
    boost::intrusive_ptr原理介绍
    shared_ptr 的使用及注意事项
    小感
    JQ对页面中某个DIV的大小变化进行监听
    使用java Apache poi 根据word模板生成word报表
    字节byte自适应转换为B、KB、MB、GB、TB
    jq实现 元素显示后 点击页面的任何位置除元素本身外 隐藏元素
  • 原文地址:https://www.cnblogs.com/duhong0520/p/13284022.html
Copyright © 2020-2023  润新知