在请求包中,如何为有效负载参数中的一个键传递多个值?
In the requests package, how to pass several values for one key in the payload params?
我正在尝试获取商品类别 ID 列表,以便我可以将它们放入 url 并抓取产品信息。我知道如何将单个值放入键中,例如:
payload = {'catID': 'ID_V2L0_65'}
但是当我有很多 catID 时,我很困惑。这是我的一些代码:
navi_info = requests.get('https://shopee.co.id/api/v4/recommend/recommend?bundle=top_sold_product_microsite&limit=20&offset=0')
catIDs = [catID for catID in navi_info.json['data']['sections']['index']['key']]
payload = {'catID': catIDs[0]}
r = requests.get('https://shopee.co.id/top_products', params=payload)
“index”键嵌套在 JSON 消息中的一个数组中(“sections”的值是一个数组)。
这个问题的解决方案可能是:
navi_info = requests.get('https://shopee.co.id/api/v4/recommend/recommend?bundle=top_sold_product_microsite&limit=20&offset=0')
print(navi_info.json()['data']['sections'])
# extracts all the "index" data from all "sections"
index_arrays = [object_['index'] for object_ in navi_info.json()['data']['sections']]
index_array = index_arrays[0] # only one section with "index" key is present
# extract all catIDs from the "index" payload
catIDs = [object_['key'] for object_ in index_array]
payload = {'catID': catIDs}
print(payload)
负载应该看起来像 {'catID': ['ID_V2L0_65', 'ID_V2L0_3693', 'ID_V2L0_2', 'ID_V2L0_19', 'ID_V2L0_75', 'ID_V2L0_4040',...]}
我正在尝试获取商品类别 ID 列表,以便我可以将它们放入 url 并抓取产品信息。我知道如何将单个值放入键中,例如:
payload = {'catID': 'ID_V2L0_65'}
但是当我有很多 catID 时,我很困惑。这是我的一些代码:
navi_info = requests.get('https://shopee.co.id/api/v4/recommend/recommend?bundle=top_sold_product_microsite&limit=20&offset=0')
catIDs = [catID for catID in navi_info.json['data']['sections']['index']['key']]
payload = {'catID': catIDs[0]}
r = requests.get('https://shopee.co.id/top_products', params=payload)
“index”键嵌套在 JSON 消息中的一个数组中(“sections”的值是一个数组)。
这个问题的解决方案可能是:
navi_info = requests.get('https://shopee.co.id/api/v4/recommend/recommend?bundle=top_sold_product_microsite&limit=20&offset=0')
print(navi_info.json()['data']['sections'])
# extracts all the "index" data from all "sections"
index_arrays = [object_['index'] for object_ in navi_info.json()['data']['sections']]
index_array = index_arrays[0] # only one section with "index" key is present
# extract all catIDs from the "index" payload
catIDs = [object_['key'] for object_ in index_array]
payload = {'catID': catIDs}
print(payload)
负载应该看起来像 {'catID': ['ID_V2L0_65', 'ID_V2L0_3693', 'ID_V2L0_2', 'ID_V2L0_19', 'ID_V2L0_75', 'ID_V2L0_4040',...]}