forked from ecthros/uncaptcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththesaurus.py
31 lines (25 loc) · 790 Bytes
/
thesaurus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import requests
from bs4 import BeautifulSoup
def _get_soup_object(url, parser="html.parser"):
return BeautifulSoup(requests.get(url).text, parser)
#Takes in a word (string) and returns a list of synonyms
def getSynonyms(term, formatted=False, max=100):
if len(term.split()) > 1:
print("Error: A Term must be only a single word")
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) > max:
terms = terms[:max:]
li = []
for t in terms:
li.append(t.select("span.text")[0].getText())
if formatted:
return {term: li}
return li
except:
print("{0} has no Synonyms in the API".format(term))
#EXAMPLE
#for wrd in getSynonyms("car"):
# print wrd