【问题标题】:Creating an API request with specific parameters使用特定参数创建 API 请求
【发布时间】:2020-07-21 03:35:33
【问题描述】:

目前我正在使用以下代码为页面上的所有鞋子抓取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?我需要更改哪个参数?

【问题讨论】:

  • 我运行了你的脚本,它提供了男性和女性
  • @bigbounty 检查我的新编辑,谢谢

标签: python python-3.x api web-scraping python-requests


【解决方案1】:

@Greg 我在 Postman 中运行了您提供的 API 链接,并为 menwomen 获得了不同的结果。我在查询字符串参数中所做的所有更改是 UUIDs 这在两种情况下都是唯一的 men 它是 uuids:0f64ecc7-d624-4e91-b171-b83a03dd8550 ,16633190-45e5-4830-a068-232ac7aea82c女性 uuid:16633190-45e5-4830-a068-232ac7aea82c,193af413-39b0-4d7e-ae34-558821c381d26, -acc6-4452-9e07-39c2ca77ba32.

如果您在查询字符串中传递这 2 个唯一的 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 是唯一的。此外,如果您愿意,您可以更改国家、语言等以获取更多数据。请同时参考屏幕截图。

男士

女性

【讨论】:

  • 是否有关于 url 参数 api.nike 句柄的任何信息?我需要提取所有产品,按性别和类别过滤
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多