Слово PyDictionary "не имеет синонимов в API"
Это то, что я сделал в 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 и как мне это сделать?
4 ответа
Это обновленная версия ответа Сарана Роя:
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))
обновленная версия ofekcohen'answer
def synonyms(term):
response = requests.get('https://www.thesaurus.com/browse/{}'.format(term))
soup = BeautifulSoup(response.text, 'html.parser')
soup.find('section', {'class': 'css-191l5o0-ClassicContentCard e1qo4u830'})
return [span.text for span in soup.findAll('a', {'class': 'css-1kg1yv8 eh475bn0'})]
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')]
Вы можете добавить некоторую обработку ошибок.
Попробуй это:
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"))
Это просто модифицированная версия ответа Натана Верземниекса.