Let's Code a CLI-Dictionary in Python
From past years python has grown its popularity. Some of the reasons being:
- Python Is backed by a large community.
- Python has great corporate sponsors. One of them is Google.
- Python has amazing library for almost everything. You can look a theme here.
- It can be used for big data and cloud services at the enterprise level.
Now let's start coding
This is what we are going to build
These are our imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import sys | |
from bs4 import BeautifulSoup as bs |
- requests: Library for handling HTTP requests or simply getting a response from the website.
- bs4: It's a python library for parsing HTML tags from the source we get from requests.
- sys: It's basically a built-in library which I used for parsing the command line arguments.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
base_url = 'https://www.dictionary.com/browse/' | |
try: | |
querry = sys.argv[1] | |
except: | |
print("Enter The World !") | |
exit(-1) | |
try: | |
r = requests.get(base_url+str(querry)) | |
except: | |
print('Something Wrong ! Please check your internet connection') | |
exit(-1) |
sys.argv[1] will give us the first word given in the argument.
Again I added a try/except block in which I used requests.get() method which takes a URL as an argument and returns a response. If it doesn't get a response then it prompts the user to check internet connection.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
soup = bs(r.content, 'html.parser') | |
try: | |
adj = soup.find('span', attrs={'class':'luna-pos'}) | |
definitions = soup.find_all("ol") | |
meanings = definitions[0].findChildren("li", recursive=False) | |
except: | |
print("Word Not Found !") | |
exit(-1) |
- r.content: It is the complete source code of the website we get as a response.
- html.parser: It is the parser which will parse the HTML tags for us from the page source.
We are interested in getting the noun tag and the definitions are given for the word. Now to scrape we have to inspect the elements.
The noun tag is bounded by a span of class luna-pos.
And the definitions are in an ordered list in which the items are as a list item.
So I defined a variable call adj which will hold the adjective or noun tag. For this, I used the soup.find() method. Its first argument is the tag you want to find, for this case 'span' and the second argument is a dictionary which defines it's attributed in this case it's class 'luna-pos'.
For finding the definitions I used soup.findAll() method and passed "ol" as an argument to get all the content of ol tag and stored it in a variable called definitions. To find the elements inside it i.e the "li" tags I used findchildren() method and set "recursive=False" so that it doesn't find the child of the child.
Lastly, I wrapped all this around a try/except block for exception handling if the code is not able to get these html tags then it will simply print "Word Not found" and exit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print(querry, " ("+adj.text+")") | |
for i in range(len(meanings)): | |
print(i+1,".", meanings[i].text) |
To make it available everywhere in your terminal do the following :
- Open .bashrc file .
- add "alias define="~/path_to_your_python_script" and save.
- From terminal now run " source ~/.bashrc ".
- Now you can use " define word-to-search " anywhere in terminal.
Let's Code a CLI-Dictionary in Python
Reviewed by Rahul
on
January 11, 2019
Rating:

Good post ! keep uo
ReplyDeleteUh did It bro✌️keep�� on it....
ReplyDeleteThanks a lot !!
DeleteThanks 😊 ! Please follow the blog for more such content . codewithrahulbera.blogspot.com
ReplyDelete