在请求包中,如何为有效负载参数中的一个键传递多个值?

我正在尝试获取商品类别 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)


GCT1015
浏览 102回答 1
1回答

开满天机

“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" payloadcatIDs = [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',...]}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python