#!/usr/bin/env python3 from bs4 import BeautifulSoup, Comment import requests import sys import re def lookup(word): r = requests.get("https://www.oxforddictionaries.com/definition/english/" + word) soup = BeautifulSoup(r.text, "html.parser") entry = "" for res in soup.find_all("section", class_="senseGroup"): entry += res.find_all("span", class_="partOfSpeech")[0].get_text() # remove comments for el in res(text=lambda text: isinstance(text, Comment)): el.extract() # remove annyoing link for el in res.find_all("a", class_="moreInformationSynonyms") + \ res.find_all("a", class_="moreInformationExemples"): el.extract() for sense in res.find_all("div", class_="msDict"): # transform div structure into 'examples'-like list for el in sense.find_all(class_="entrySynList"): el.name = "ul" for syno in el.find_all("div"): syno.name = "li" #el.insert_before(soup.new_tag("br")) # simplify structure for el in sense.find_all("span") + \ sense.find_all("div"): # skip "more" tags if "onclick" not in el.attrs: el.unwrap() # remove unused classes for el in sense.find_all(): del el["class"] del sense["class"] # hide examples for el in sense.find_all("li"): el["style"] = "font-size: 75%;" entry += str(sense) #.prettify(formatter="html") #entry += "
" entry += "
" # ugly hack. Why is this necessary: #- for el in sense.find_all("a", class_="moreInformationExemples"): #- AttributeError: 'NoneType' object has no attribute 'next_element' soup = BeautifulSoup(entry, "html.parser") for el in soup.find_all("li"): if "Get more examples" == el.get_text() or \ "View synonyms" in el.get_text() or \ el.get_text() == "": el.extract() text = str(soup) # another hack, I'm out of time text = re.sub(r"(\d)([A-Za-z])", r"\1 \2", text) return text #lookup("clerk") print(lookup("smudge"))