使用特定参数创建API请求

2024-10-01 09:20:33 发布

您现在位置:Python中文网/ 问答频道 /正文

目前,我正在使用以下代码为页面上的所有鞋刮https://www.nike.com/w/mens-shoes-nik1zy7ok

import requests
import json

# I used a placeholder for the anchor parameter
uri = 'https://api.nike.com/cic/browse/v1?queryid=products&country=us&endpoint=product_feed/rollup_threads/v2?filter=marketplace(US)%26filter=language(en)%26filter=employeePrice(true)%26filter=attributeIds(0f64ecc7-d624-4e91-b171-b83a03dd8550%2C16633190-45e5-4830-a068-232ac7aea82c)%26anchor={}%26consumerChannelId=d9a5bc42-4b9c-4976-858a-f159cf99c647%26count=60'

# collect all products
store = []
with requests.Session() as session:
    found_all_products = False
    anchor = 0
    while not found_all_products:
        result = session.get(uri.format(anchor)).json()
        products = result['data']['products']['products']
        store += products

        if len(products) < 60:
            found_all_products = True
        else:
            anchor += 24

# filter by cloudProductId to get a dictionary with unique products
cloudProductIds = set()
unique_products = []
for product in store:
    if not product['cloudProductId'] in cloudProductIds:
        cloudProductIds.add(product['cloudProductId'])
        unique_products.append(product)

我如何编写相同的api请求从该站点检索男鞋或在女鞋页面检索女鞋:https://www.nike.com/w/womens-shoes-5e1x6zy7ok?我需要更改哪个参数


Tags: storehttpscomwww页面allproductproducts
1条回答
网友
1楼 · 发布于 2024-10-01 09:20:33

@Greg我在《邮差》中运行了你提供的API链接,得到了不同的结果,分别针对男性女性。我在查询字符串参数中所做的所有更改都是UUIDs,这在男性和女性的情况下都是独一无二的:0f64ecc7-d624-4e91-b171-B83A03DD855016633190-45e5-4830-a068-232ac7aea82cUUIDs:16633190-45e5-4830-a068-232ac7aea82c,193af413-39b0-4d7e-ae34-558821381d3f,7baf216c-acc6-4452-9e07-39c2ca77ba32

如果您在查询字符串中传递这两组唯一的UUID,那么您将分别得到男性和女性的结果,因为没有其他参数来定义他们的身份

代码如下:

import json
import requests

#common query parameters
queryid = 'filteredProductsWithContext'
anonymousId = '25AFE5BE9BB9BC03DE89DBE170D80669'
language = 'en-GB'
country = 'IN'
channel = 'NIKE'
localizedRangeStr = '%7BlowestPrice%7D%E2%80%94%7BhighestPrice%7D'

#UUIDs
uuids_men = '0f64ecc7-d624-4e91-b171-b83a03dd8550,16633190-45e5-4830-a068-232ac7aea82c'
uuids_women = '16633190-45e5-4830-a068-232ac7aea82c,193af413-39b0-4d7e-ae34-558821381d3f,7baf216c-acc6-4452-9e07-39c2ca77ba32'

def get_men_result():
    url = 'https://api.nike.com/cic/browse/v1?queryid=' + queryid + '&anonymousId=' + anonymousId + '&uuids=' + uuids_men + '&language=' + language + '&country=' + country + '&channel=' + channel + '&localizedRangeStr=' + localizedRangeStr
    data = requests.get(url,verify = False).json()
    print(data)

def get_women_result():
    url = 'https://api.nike.com/cic/browse/v1?queryid=' + queryid + '&anonymousId=' + anonymousId + '&uuids=' + uuids_women + '&language=' + language + '&country=' + country + '&channel=' + channel + '&localizedRangeStr=' + localizedRangeStr
    data = requests.get(url,verify = False).json()
    print(data)

get_men_result()
print('-'*100)
get_women_result()

如果您查看我为男性和女性创建的查询字符串,您会注意到有6个公共参数,只有uuid是唯一的。如果您愿意,还可以更改国家、语言等以获取更多数据。请同时参考屏幕截图

男人Men postman resultMen Actual result

女性Women postman resultWomen actual result

相关问题 更多 >