代码:
import time
import giphy_client
from giphy_client.rest import ApiException
from pprint import pprint
def giphyapi():
api_instance = giphy_client.DefaultApi()
api_key = '################################'
tag = 'test'
rating = 'pg-13'
fmt = 'json'
try:
# Search Endpoint
api_response = api_instance.gifs_random_get(api_key, tag = tag, rating = rating, fmt = fmt)
## here’s where I want to do stuff with the data
except ApiException as exc:
print("Exception when calling DefaultApi->gifs_random_get: %s\n" % exc)
return None
giphyapi()
嗨!如何将api_instance
转换成可操作的东西,例如dict?
这与this是同一个问题,但作者遗憾地发现,这个问题的解决方案对我不起作用
我已经尝试了print(api_response.data[0].images.type)
,但这引发了以下错误:
TypeError:“RandomGif”对象不可下标
我也试过:
for block in api_response["data"]:
giftype = block["type"]
但这引发了这个错误: TypeError:“InlineResponse2002”对象不可下标
我使用的是Python 3.8.1,我还使用了giphy-python-clientHere是RandomGif模型的列表。我在上面尝试的两个例子中试图获取的是type
非常感谢您的帮助!🙂
多亏了Reddit上的shoot2thr1ll284,我才解决了这个问题
只需使用
api_response.data.type
并将type
替换为要获取的属性相关问题 更多 >
编程相关推荐