如何使用 python 脚本从 merriam-webster 获取特定单词的定义? 我有带有文本框和按钮的窗口,我想在屏幕上打印单词的定义。 谢谢
请您参考如下方法:
# Import libraries/ modules
import logging
logging.basicConfig(level = logging.INFO)
import json
import requests
# write custom definition
def connect_mw_dictionary(api_key, word):
logging.info("Connecting...")
URL = "https://www.dictionaryapi.com/api/v3/references/sd2/json/"+word+"?
key="+api_key
PARAMS = {'word': word,'key': api_key}
r = requests.get(url = URL, params = PARAMS)
r.encoding = 'utf-8'
if r.status_code == 200:
logging.info("Connection successful.")
return True, r.json()
logging.info("Connection failed.")
return False
def fetch_dictionary_result(res, lang, loc, audio_format, word):
dict_txt = str(res[0]['def'][0]['sseq'][0][0][1]['dt'][0][1]).replace("
{bc}","")
return dict_txt
api_key = "xxxxxxxxxxxx" # replace with you api key
word = "keyword" # replace with word to lookup
lang = "en"
loc = "us"
audio_format = "mp3"
status, result = connect_mw_dictionary(api_key, word)
if status == True:
status, dictionary_result = fetch_dictionary_result(result, lang, loc,
audio_format, word)
字典结果
