현재 요청 후 응답에서 일부 데이터를 구문 분석하려고하는데 "TypeError : list indices must be integers or slices, not str"오류가 계속 발생합니다.
Python 코드
import requests
import json
count = 0
params = {'var1':'40', 'value':'143', 'itm':'1', 'param':'1'}
req = 'https://www.api.com/api/search'
data = requests.post(req, data = params).json()
print (data['result']['results']['name'])
JSON 응답
{
"result":{
"count":1,
"totalCount":1,
"offset":0,
"queryTime":232,
"results":[
{
"rating":"4.0",
"productId":{
"upc":"143",
"ItemId":"143",
"productId":"143-prd"
},
"name":"Product",
"catagory":{
"name":"",
"CataId":1
},
"images":{
"thumbnailUrl":"http://api.com/img/static/product-image-50-50.png",
"largeUrl":"http://api.com/img/static/product-image-500-500.png"
},
"price":{
"price":13,
"isRealTime":true,
"currencyUnit":"USD"
},
"location":{
"unit":[],
"detailed":[]
},
"inventory":{
"quantity":1,
"status":"In Stock",
"isRealTime":true
},
"ratings":{
"rating":"3.1875",
"ratingUrl":"http://api.com/3_1875.gif"
},
"reviews":{
"reviewCount":"2"
},
"isItem":true,
"lUrl":"/l/Product-Name"
}
],
"performance":{
"enrichment":{
}
},
"query":{
"originalQuery":"143",
"actualQuery":"143",
"suggestedQueries":[
]
},
"algo":"jarvis",
"blacklist":false,
"cluster":{
"apiserver":{
"hostname":"site.api.com",
"pluginVersion":"1.0"
},
"searchengine":{
"hostname":"srch.site.api.com"
}
}
}
}
비슷한 코드를 작성했지만 요청을 받았으며 모든 것이 잘되었습니다.
data['result']['results']
배열이므로 ['name']
int가 필요 하지 않으며 [0]
나중에 추가 할 수 있으며 ['results']
작동해야합니다. 그런 다음에서 객체 내의 키를 참조 할 수 있습니다 results
.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다