这就是我在ipython中所做的(我正在使用Python 3.6)

from PyDictionary import PyDictionary
dictionary = PyDictionary()
list = dictionary.synonym("life")

我得到了错误:
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PyDictionary/utils.py:5: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html5lib"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.

The code that caused this warning is on line 5 of the file /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PyDictionary/utils.py. To get rid of this warning, pass the additional argument 'features="html5lib"' to the BeautifulSoup constructor.

  return BeautifulSoup(requests.get(url).text)
life has no Synonyms in the API

我尝试过的每个单词都会发生这种情况,这是我做错了吗?是我需要添加参数'features =“html5lib”'的问题,如果是,那么BeautifulSoup构造函数在哪里,我该怎么做?

最佳答案

它是Saran Roy答案的更新版本:

import requests
from bs4 import BeautifulSoup

def synonyms(term):
    response = requests.get('https://www.thesaurus.com/browse/{}'.format(term))
    soup = BeautifulSoup(response.text, 'lxml')
    soup.find('section', {'class': 'css-191l5o0-ClassicContentCard e1qo4u830'})
    return [span.text for span in soup.findAll('a', {'class': 'css-r5sw71-ItemAnchor etbu2a31'})] # 'css-1k3kgmb-ItemAnchor etbu2a31' for less relevant synonyms

word = "Input Your Word Here!"
print(synonyms(word))

关于python - PyDictionary字 "has no Synonyms in the API",我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52910297/

10-12 17:01