Skip to content

Instantly share code, notes, and snippets.

@rafa2000
Forked from schuhumi/get_wotd.py
Last active June 1, 2020 16:55
Show Gist options
  • Save rafa2000/4ef2439b7be876f06303a04cf58b9669 to your computer and use it in GitHub Desktop.
Save rafa2000/4ef2439b7be876f06303a04cf58b9669 to your computer and use it in GitHub Desktop.
Get Word of the Day from dictionary.com https://imgur.com/a/YicwTP6
#!/usr/bin/env python3
import urllib3
from bs4 import BeautifulSoup
from os import path
class bcolors:
HEADER = '\033[95m'
CYAN = '\033[36m'
BLUE = '\033[34m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
ITALIC = '\033[3m'
DIM = '\033[2m'
http = urllib3.PoolManager()
try:
page_url = "https://www.dictionary.com/e/word-of-the-day/"
page = http.request('GET', page_url).data
soup = BeautifulSoup(page, "html.parser")
wotd_div = soup.find("div", class_="wotd-item-headword")
wotd_word = wotd_div.find("div", class_="wotd-item-headword__word").find("h1").text
wotd_pos_blocks = wotd_div.find("div", class_="wotd-item-headword__pos").find_all("p")
wotd_type = wotd_pos_blocks[0].find("span", class_="luna-pos").text
wotd_explanation = wotd_pos_blocks[1].text
wotd_pronunciation = wotd_div.find("div", class_="wotd-item-headword__pronunciation").find("div").text.strip()
line1 = f"{bcolors.CYAN}{bcolors.BOLD}{wotd_word}{bcolors.ENDC} {wotd_pronunciation}: {bcolors.DIM}{bcolors.ITALIC}{wotd_type}{bcolors.ENDC}"
line2 = f"{wotd_explanation}"
with open(path.join(path.expanduser("~"), ".wotd"), "w") as f:
f.write(f" {line1}\n {line2}\n")
except Exception as e: # Word of the day stays the same if something fails, e.g. no internet
print("failed to get word of the day: ", e)
pass
@rafa2000
Copy link
Author

rafa2000 commented Jun 1, 2020

Updated for 'urllib3' on Python 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment