记录#
- 今天继续学习 request 和 json 知识点。
- 使用
headers={"Accept":"application/json"}
参数,指定返回内容的格式。 - 今天的练习是从
[icanhazdadjoke](https://icanhazdadjoke.com/)
获取并展示一个 joke,询问用户是否需要保存、下一个的操作。
CODE#
main.py#
import requests, json
from replit import db
while True:
result = requests.get("https://icanhazdadjoke.com/",
headers={"Accept": "application/json"})
joke = result.json()
id = joke["id"]
text = joke["joke"]
print(text)
print()
menu = input("(s)ave joke, (l)oad old jokes, (n)ew joke\n")
if menu == "s":
db[id] = {"joke": text}
elif menu == "l":
for key in db.keys():
print(db[key]["joke"])
print()