記錄#
- 今天繼續學習 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)new joke\n")
if menu == "s":
db[id] = {"joke": text}
elif menu == "l":
for key in db.keys():
print(db[key]["joke"])
print()