diff --git a/PyDictionary/core.py b/PyDictionary/core.py index e63d8dc..65b8c88 100644 --- a/PyDictionary/core.py +++ b/PyDictionary/core.py @@ -1,5 +1,5 @@ from __future__ import print_function -import sys, re, goslate +import sys, re, goslate, json try: from .utils import _get_soup_object except: @@ -19,7 +19,7 @@ def __init__(self, *args): except: self.args = args - + def printMeanings(self): dic = self.getMeanings() for key in dic.keys(): @@ -69,12 +69,13 @@ def synonym(term, formatted=False): else: try: data = _get_soup_object("http://www.thesaurus.com/browse/{0}".format(term)) - terms = data.select("div#filters-0")[0].findAll("li") - if len(terms) > 5: - terms = terms[:5:] + re_synonyms = re.search('"synonyms":(\[.*?\])', data.get_text()).group(1) + synonyms = json.loads(re_synonyms) + if len(synonyms) > 5: + synonyms = synonyms[:5:] li = [] - for t in terms: - li.append(t.select("span.text")[0].getText()) + for s in synonyms: + li.append(s["term"]) if formatted: return {term: li} return li @@ -100,12 +101,13 @@ def antonym(word, formatted=False): else: try: data = _get_soup_object("http://www.thesaurus.com/browse/{0}".format(word)) - terms = data.select("section.antonyms")[0].findAll("li") - if len(terms) > 5: - terms = terms[:5:] + re_antonyms = re.search('"antonyms":(\[.*?\])', data.get_text()).group(1) + antonyms = json.loads(re_antonyms) + if len(antonyms) > 5: + antonyms = antonyms[:5:] li = [] - for t in terms: - li.append(t.select("span.text")[0].getText()) + for a in antonyms: + li.append(a["term"]) if formatted: return {word: li} return li