这就是我在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构造函数在哪里,我该怎么做?
答案 0 :(得分:1)
它是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))
答案 1 :(得分:0)
PyDictionary.synonym
函数尝试在thesaurus.com上查找同义词,但是代码已过期。它正在寻找不再存在的html结构。以下代码将基本完成相同的操作:
import requests
from bs4 import BeautifulSoup
def synonyms(term):
response = requests.get('http://www.thesaurus.com/browse/{}'.format(term))
soup = BeautifulSoup(response.text, 'html')
section = soup.find('section', {'class': 'synonyms-container'})
return [span.text for span in section.findAll('span')]
您可能要添加一些错误处理。
答案 2 :(得分:0)
尝试一下:
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': 'synonyms-container'})
return [span.text for span in soup.findAll('a', {'class': 'css-18rr30y'})] # class = .css-7854fb for less relevant
print(synonyms("reticulum"))
它只是NathanVērzemnieks答案的修改版本。