小编upy*_*yop的帖子

how to find out if string is in the dictionary

I want to know if a word is in the dictionary.

Here is what I am trying.

import requests

def word_in_dictionary(word):
    response = requests.get('https://en.wiktionary.org/wiki/'+word)
    return response.status_code==200

print(word_in_dictionary('potato')) # True
print(word_in_dictionary('nobblebog')) # False
Run Code Online (Sandbox Code Playgroud)

But unfortunately the dictionary contains a lot of words that are not English and I don't want to match those.

print(word_in_dictionary('bardzo')) # WANT THIS TO BE FALSE
Run Code Online (Sandbox Code Playgroud)

So I tried to look in the content.

def word_in_dictionary(word):
    response = requests.get('https://en.wiktionary.org/wiki/'+word)
    return response.status_code==200 and 'English' in response.content.decode()
Run Code Online (Sandbox Code Playgroud)

But I …

python python-requests

3
推荐指数
1
解决办法
64
查看次数

标签 统计

python ×1

python-requests ×1