Skip to content

Instantly share code, notes, and snippets.

@SouravJohar
Created November 4, 2018 17:56
Show Gist options
  • Save SouravJohar/ef821d570d1922e50ce07eeadb361d53 to your computer and use it in GitHub Desktop.
Save SouravJohar/ef821d570d1922e50ce07eeadb361d53 to your computer and use it in GitHub Desktop.
'''
Note: Using Python 2 may result in unicode errors
'''
import sys
import requests
from bs4 import BeautifulSoup as bs
url = "https://www.dictionary.com/browse/"
# read the word from the command line arguments and append to the url
try:
word = sys.argv[1]
url += word
except:
print("Specify a word!")
exit(-1)
# load the website's source code
try:
r = requests.get(url)
soup = bs(r.content, 'lxml')
except:
print("You're probably not connected to the internet!")
exit(-1)
# parse the source to obtain all necessary info
try:
header = soup.findAll("span", {"class": "luna-pos"})[0].text
answer_list = soup.findAll("ol")[0]
meanings = answer_list.findChildren("li", recursive=False)
except:
print("Word not found!")
exit(-1)
# display the results
print()
print(word + ": " + header)
for (i, meaning) in enumerate(meanings):
print()
print(str(i + 1) + ".", meaning.text)
@majdalrifai
Copy link

How would I make this work with Python 3?

@Dhyey17
Copy link

Dhyey17 commented Jan 15, 2020

I cannot install the lxml parser
It says me to install c++ 14.0 but i already have c++ 14.28 install on my laptop

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