use*_*890 5 python python-3.x pydictionary
这就是我在ipython中所做的(我使用的是Python 3.6)
from PyDictionary import PyDictionary
dictionary = PyDictionary()
list = dictionary.synonym("life")
Run Code Online (Sandbox Code Playgroud)
我收到错误:
/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
Run Code Online (Sandbox Code Playgroud)
对于我尝试的每个单词都会发生这种情况,我做错了什么?我需要添加参数'features ="html5lib"'的问题,如果是,那么BeautifulSoup构造函数在哪里,我该怎么做?
该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')]
Run Code Online (Sandbox Code Playgroud)
您可能想要添加一些错误处理。
| 归档时间: |
|
| 查看次数: |
570 次 |
| 最近记录: |