im试图访问下面的json示例的nodes
部分。
{
"pages": {
"prev": "",
"next": "/product_feed/threads/v2?filter=marketplace%28US%29&filter=language%28en%29&filter=channelId%28010794e5-35fe-4e32-aaff-cd2c74f89d61%29&anchor=50",
"totalPages": 48,
"totalResources": 2385
},
"objects": [
{
"id": "146e9350-97d9-4dd4-ac3a-c1d85fe49021",
"channelId": "010794e5-35fe-4e32-aaff-cd2c74f89d61",
"channelName": "SNKRS WEB",
"marketplace": "US",
"language": "en",
"lastFetchTime": "2018-08-01T22:53:04.874Z",
"publishedContent": {
"marketplace": "US",
"collectionGroupId": "010794e5-35fe-4e32-aaff-cd2c74f89d61",
"createdDateTime": "2018-08-01T22:52:34.423Z",
"language": "en",
"viewStartDate": "2018-08-01T22:50:00.000Z",
"type": "thread",
"version": "1533163903286",
"classifications": [
],
"nodes": [
{
"subType": "image",
"id": "a06c8da1-e700-45fa-8bb4-9b793dc34f9f",
"type": "card",
"version": "1533163901597",
"properties": {
"portraitId": "hw6nubnjepxt1gevqcyb",
"squarishURL": "",
"product": [
],
"landscapeId": "xpvylaxdn1svszos2ocq",
"altText": "Nike Moon Racer 'Black & White & Wolf Grey' Release Date",
"portraitURL": "https://c.static-nike.com/a/images/w_1536,c_limit/hw6nubnjepxt1gevqcyb/nike-moon-racer-black-white-wolf-grey-release-date.jpg",
"landscapeURL": "https://c.static-nike.com/a/images/w_1920,c_limit/xpvylaxdn1svszos2ocq/nike-moon-racer-black-white-wolf-grey-release-date.jpg",
"portrait": {
"aspectRatio": 0.66667,
"id": "hw6nubnjepxt1gevqcyb",
"type": "editorial",
"url": "https://c.static-nike.com/a/images/w_1536,c_limit/hw6nubnjepxt1gevqcyb/nike-moon-racer-black-white-wolf-grey-release-date.jpg"
这是我当前拥有的代码,无法获取json的objects
部分,但是每次尝试获取节点时,都会遇到str错误。 TypeError: list indices must be integers or slices, not str
import requests
import bs4
import json
import time
endpoint = "https://api.nike.com/product_feed/threads/v2/?filter=marketplace%28US%29&filter=language%28en%29&filter=channelId%28010794e5-
35fe-4e32-aaff-cd2c74f89d61%29"
req = requests.get(endpoint)
reqJson = json.loads(req.text)
for object in reqJson['objects']['nodes']:
print (object)
答案 0 :(得分:0)
该错误是因为“对象”指向一个列表:您需要该列表的元素0,然后使用“ publishedContent”,然后使用“ nodes”。使用:
reqJson["objects"][0]["publishedContent"]["nodes"]