简体   繁体   中英

PyDictionary word "has no Synonyms in the API"

This is what I did in ipython (I'm using Python 3.6)

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

And I get the error:

/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

This happens for each word I've tried, am I doing something wrong? Is the issue that I need to add the argument 'features="html5lib"', and if it is, where is the BeautifulSoup constructor and how do I do this?

It is an updated version of Saran Roy's answer:

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))

The PyDictionary.synonym function tries to look up synonyms on thesaurus.com, but the code is out of date. It's looking for html structures that don't exist anymore. The following code will do basically the same thing:

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')]

You may want to add some error handling.

Try this:

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"))

Its just a modified version of Nathan Vērzemnieks's answer.

updated version of 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'})] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM