首页 > 解决方案 > 将查询字符串参数传递给请求调用不起作用

问题描述

我正在尝试使用此 python 脚本获取此 [产品 url][1] 的 json 响应。我查看了站点查询参数并从开发工具中添加了网络选项卡。

我使用了类似的方法 [here][2] 来获取查询的 json 响应,但是当我尝试使用下面的代码时,它产生了错误。这是代码:

def getIndividualItemData():
    print("fetching ")

    
    api_url =  'https://www.walmart.com/product/electrode/api/wpa'
    params = {
        
       
        'el': 'sponsored-container-middle-2',
        'type': 'product',
        'min': '3',
        'max': '15',
        'placementId': '480x345_B-C-OG_TI_2-15_HL-BOTTOM-NY',
        'platform': 'mobile',
        'bucketId':"",
        'bnd': 'Omid Scobie', 
        'moduleLocation': 'bottom',
        'zipCode': '19146',
        'isZipLocated': 'true',
        'sMode': '0',
        'pageType': 'item',
        'vtc': 'QrqzIuiG6-yciq_CD4uAJg',
        'uid': 'daa00990-dab1-4257-9705-3d4a31be8aee',
        'rviItems': '915257222', 
        'itemsAddedToCart': '0',
        'viewportHeight': '616',
        'viewportWidth': '150',
        'userLoggedIn': 'false',
        'showBrand': 'false',
        'itemsList':"" ,
        'pageId': '915257222',
        'keyword': 'Finding Freedom Harry and Meghan and the Making of a Modern Royal Family Hardcover',
        'taxonomy': '3920_5196470_2244664',
        'persistControls': 'true',
        'isTwoDayDeliveryTextEnabled': 'true',
        'mloc': 'bottom',
        'module': 'wpa'

    }


    try:

         try:
            data = requests.get(api_url, params=params).json()

         except Exception as e:
            print(e)
            


    except Exception as e:
        print(e)
     

我不断打印出这个错误

期望值:第 1 行第 1 列(字符 0)

是什么导致了错误,如何获取产品 json 响应?

我尝试了 .text 打印,它只显示很少的数据 [1]:https ://www.walmart.com/ip/2-Pack-Maxwell-House-Original-Roast-Medium-Ground-Coffee-Caffeinated -42-5-oz-Canister/915257222 [2]:Python:如何为 Python 请求设置参数

标签: pythonweb-scrapingpython-requests

解决方案


推荐阅读