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
- 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.
Now I created a variable called base_url which will hold the URL for the website and added an exception handling block to check if the word is provided or not. Here querry variable holds
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.
Now I created a soup object by passing it two arguments :
- 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.
At last, I printed out the word and the adjective(tag) for the word and iterated over the meanings scraped and printed them out. Let's run the code ;)
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