【发布时间】:2022-01-09 11:11:12
【问题描述】:
我想构建一个简单的应用程序,该应用程序将从城市词典 api 生成随机单词及其相关定义。我在想我可以以某种方式抓取网站或找到包含大多数城市词典单词的数据库或 .csv 文件,然后将其注入 api {word}。
我在这里找到了他们的非官方/官方 API:http://api.urbandictionary.com/v0
这里有更多关于它的信息:https://pub.dev/documentation/urbandictionary/latest/urbandictionary/OfficialUrbanDictionaryClient-class.html 在这里:https://pub.dev/documentation/urbandictionary/latest/urbandictionary/UrbanDictionary-class.html
在第二个 pub.dev 链接中,似乎有一个内置函数,可以从站点生成随机单词列表。因此,显然不必找到数据库/网络刮掉单词,这将是创建此应用程序的更好方法。问题是我不知道如何在我的代码中调用该函数。
API 新手,目前我的代码如下:
import requests
word = "all good in the hood"
response = requests.get(f"http://api.urbandictionary.com/v0/define?term={word}")
print(response.text)
这在 VSCODE 中给出了一个长的 JSON/字典。如果可以访问该随机函数并从列表中获取一个随机单词,我想我可以扩展这个想法。
感谢任何帮助。
谢谢
【问题讨论】:
-
@Selcuk 是的,就是这样......没有意识到这有多容易......我如何从 JSON 中提取单词和定义?当我打印(response.JSON())时,我得到了这一切:
{'list': [{'definition': 'A classic rave/club-whore term that refers to a brief nap.', 'permalink': 'http://disco-nap.urbanup.com/1087145', 'thumbs_up': 435, 'sound_urls': [], 'author': 'tfk', 'word': 'disco nap', 'defid': 1087145, 'current_vote': '', 'written_on': '2005-02-26T17:40:22.000Z', 'example': 'I [had to] [catch] a disco nap before going to the [next] party.', 'thumbs_down': 288}, {'definition': -
请向edit 学习添加这么多信息。
标签: python json api web-scraping python-requests