Skip to content

Instantly share code, notes, and snippets.

@schuhumi
Created June 1, 2020 11:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save schuhumi/8d094d038b93aace6b456d70849aae10 to your computer and use it in GitHub Desktop.
Save schuhumi/8d094d038b93aace6b456d70849aae10 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 urllib
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'
try:
page_url = "https://www.dictionary.com/e/word-of-the-day/"
page = urllib.request.urlopen(page_url)
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment