-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
42 lines (33 loc) · 1.15 KB
/
main.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
32
33
34
35
36
37
38
39
40
41
42
from bs4 import BeautifulSoup
import requests
word = 'run'
def getdata(word):
url = f'https://www.oxfordlearnersdictionaries.com/definition/english/{word}?q={word}'
data = requests.get(url).text
soup = BeautifulSoup(data, 'html.parser')
try:
is_syno = False
name = soup.h1.get_text()
pos = soup.find('span', class_= 'pos').get_text()
definition = [i.get_text() for i in soup.find_all('span', class_= 'def')]
examples = [i.get_text() for i in soup.find_all('span', class_= 'x')]
try:
if soup.find('span', class_= 'prefix').get_text().lower() == 'synonym':
syno = soup.find('span', class_= 'xh').get_text()
is_syno =True
except:
is_syno = False
filter_data = {
'word': name,
'pos': pos,
'definition': definition,
'examples': examples,
'code': 200
}
if is_syno:
filter_data["synonyms"] = syno
except:
filter_data = {'status': 'word dose not found', 'code': 404}
return filter_data
if __name__ == '__main__':
print(getdata('demoo'))