|
| 1 | +import time |
| 2 | +import aiohttp |
| 3 | +import asyncio |
| 4 | +from json import dumps |
| 5 | +from random import choice |
| 6 | +from bs4 import BeautifulSoup as Beau_Soup |
| 7 | + |
| 8 | +def get_syn_ant(word: str, page: str): |
| 9 | + soup = Beau_Soup(page,'html.parser') |
| 10 | + synonyms_list = list() |
| 11 | + antonyms_list = list() |
| 12 | + |
| 13 | + # getting all the synonyms |
| 14 | + for a in soup.select('ul a.css-1kg1yv8'): |
| 15 | + synonyms_list.append(a.text) |
| 16 | + |
| 17 | + for a in soup.select('ul.css-1gyuw4i'): |
| 18 | + synonyms_list.append(a.text) |
| 19 | + |
| 20 | + for a in soup.select('ul.css-1n6g4vv'): |
| 21 | + synonyms_list.append(a.text) |
| 22 | + |
| 23 | + # getting all the antonyms |
| 24 | + for a in soup.select('ul a.css-15bafsg'): |
| 25 | + antonyms_list.append(a.text) |
| 26 | + |
| 27 | + # chosing random synonym and antonym then return with the word |
| 28 | + return { |
| 29 | + 'word': word, |
| 30 | + 'synonym' : choice(synonyms_list).strip() if synonyms_list else 'No synonym', |
| 31 | + 'antonym': choice(antonyms_list).strip() if antonyms_list else 'No antonym' |
| 32 | + } |
| 33 | + |
| 34 | +async def get_page(session, word: str): |
| 35 | + # get the HTML code of the word page |
| 36 | + url_to_get = f"https://www.thesaurus.com/browse/{word}" |
| 37 | + async with session.get(url_to_get) as response: |
| 38 | + result_data = await response.text() |
| 39 | + |
| 40 | + return get_syn_ant(word, result_data) |
| 41 | + |
| 42 | + |
| 43 | +async def get_all_pages() : |
| 44 | + words_to_look_for = input("Enter the words to look for : ").split() |
| 45 | + words_to_look_for = [word.strip() for word in words_to_look_for if word != ''] |
| 46 | + |
| 47 | + tasks = list() |
| 48 | + async with aiohttp.ClientSession() as session: |
| 49 | + for word in words_to_look_for: |
| 50 | + task = asyncio.ensure_future(get_page(session, word)) |
| 51 | + tasks.append(task) |
| 52 | + |
| 53 | + return await asyncio.gather(*tasks) |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +if '__main__' == __name__: |
| 58 | + begin_time = time.time() |
| 59 | + |
| 60 | + asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) |
| 61 | + syn_ant_for_words = asyncio.run(get_all_pages()) |
| 62 | + for word_detail in syn_ant_for_words: |
| 63 | + print(dumps(word_detail, indent=4)) |
| 64 | + |
| 65 | + print("--- %s seconds ---" % (time.time() - begin_time)) |
| 66 | + |
0 commit comments