【问题标题】:How to use urbandictionary API built in API function random()如何使用 API 函数 random() 中内置的 urbandictionary API
【发布时间】: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


【解决方案1】:

抓取 Urban Dictionary 中的所有单词需要很长时间。您可以通过调用https://api.urbandictionary.com/v0/random从Urban Dictionary 中获取一个随机单词

这是一个从 Urban Dictionary 中获取随机单词的函数

def randomword():
    response = requests.get("https://api.urbandictionary.com/v0/random")

    return response.text

为了将响应转换为 JSON,您必须导入 JSON 并执行json.loads(response.text)。一旦转换为 JSON,它基本上就是一个字典。这是获取第一个定义的定义、单词和作者的代码

data = json.loads(randomword()) #gets random and converts to JSON
firstdef = data["list"][0] #gets first definition
author = firstdef["author"] #author of definition
definition = firstdef["definition"] #definition of word
word = firstdef["word"]  #word

【讨论】:

  • 感谢这完全符合预期!!我正在考虑制作一个带有深色背景和浅色字母的简单网络应用程序,并在某处带有一个按钮,用于随机单词生成器。在 python 中,你认为最好的方法是什么?
  • 你可以使用flask创建一个web应用。这是一篇关于如何做的好文章that
【解决方案2】:

参考上面的评论,我分享你需要的方法。

import requests
word = "all good in the hood"
response = requests.get(f"https://api.urbandictionary.com/v0/random")

# get all list item 
for obj in response.json()['list']:
    print(obj)

# get index 0 of list
print(response.json()['list'][0])

# get index 0 - word of list
print(response.json()['list'][0]['word'])

【讨论】:

    【解决方案3】:

    文本是json格式的,所以只需使用json模块转换成字典即可。我也有它只是给出了最多拇指的定义

    import json
    import requests
    word = "all good in the hood"
    response = requests.get(f"http://api.urbandictionary.com/v0/define?term={word}")
    dictionary = json.loads(response.text)['list']
    most_thumbs = -1
    best_definition = ""
    for definition in dictionary:
        if definition['thumbs_up']>most_thumbs:
            most_thumbs = definition['thumbs_up']
            best_definition = definition['definition']
    print(f"{word}: {best_definition}")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-13
      • 1970-01-01
      • 1970-01-01
      • 2015-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多