requests 库
Coddy 随机事实生成器:Python 项目 课程的第 3 课,共 7 课。
要“获取”(或取得)API 的内容,我们会使用名为 requests 的库。
使用 get 方法可以获取网页内容:
import requests
response = requests.get("https://someurlhere")response 对象有许多属性,我们要了解的是 text,它会返回一个包含网站内容的字符串:
import requests
response = requests.get("https://someurlhere")
content = response.text挑战
简单现在,修改函数(get_fact)的函数体,让它从 API 端点(上一步中的 URL)获取内容,并返回响应文本!
别忘了
import requests
亲手试试
def get_fact():
return 'https://uselessfacts.jsph.pl/api/v2/facts/random'随机事实生成器:Python 项目中的所有课
自己练习: Python 在线编译器