forked from tetrismegistus/etym
-
Notifications
You must be signed in to change notification settings - Fork 0
/
etym.py
34 lines (24 loc) · 1.05 KB
/
etym.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
#!/usr/bin/env python3
from bs4 import BeautifulSoup
import urllib.request
import argparse
SEARCH_URL = 'https://www.etymonline.com/search?q='
AGENT = {'User-Agent': "Magic Browser"}
def soup_search(query_url: str, search_term: str):
# post request to search URL, return beautiful soup parsed object
url = query_url + search_term
req = urllib.request.Request(url, headers=AGENT)
response = urllib.request.urlopen(req)
html = response.read()
return BeautifulSoup(html, 'html.parser')
def first_result_print(search_page):
print("{}\n".format(search_page.find("div", class_="searchList__pageCount--2jQdB").get_text()))
print(search_page.find("section", class_="word__defination--2q7ZH undefined").get_text().replace('\n', '\n\n'))
def main():
parser = argparse.ArgumentParser(prog='etym.py', usage='%(prog)s word')
parser.add_argument('word', help="The word you wish to look up")
args = parser.parse_args()
results = soup_search(SEARCH_URL, args.word)
first_result_print(results)
if __name__ == '__main__':
main()