【发布时间】:2021-04-17 14:46:13
【问题描述】:
我正在使用 Python 编写一个脚本,该脚本将使用我在网上找到的 API 生成笑话。 (https://sv443.net/jokeapi/v2/)。然而,笑话的一些设置/问题部分使用 JSON 数据,它在“设置”和“笑话”之间变化。我正在寻找是否可以编写一个脚本来检查响应是拉哪一个。我的脚本在这里:
import requests
import json
def getJoke():
response = requests.get("https://v2.jokeapi.dev/joke/Any")
print(response.text)
json_data = json.loads(response.text)
joke = json_data['joke'] + json_data['setup'] + " " + json_data['delivery']
print(joke)
getJoke()
有些回答是
"error": false,
"category": "Christmas",
"type": "twopart",
"setup": "How will Christmas dinner be different after Brexit?",
"delivery": "No Brussels!",
和
"error": false,
"category": "Programming",
"type": "twopart",
"joke": "Why is Linux safe?",
"delivery": "Hackers peak through Windows only.",
有没有办法检查响应获得的数据名称?
【问题讨论】:
标签: python json python-requests