Requests in Python
#day5 of #100daysofcode
Today I decided to explore the requests library of Python. It comes very handy in automation, scraping, making requests using HTTP methods, etc. I decided to learn to automate some boring stuff. But it will take some time. For now, I made myself comfortable with it. My aim was to make an attendance monitor. Our university has a very good online management system. Our attendance, masks, announcements and almost are done online there.
I have to open it daily to check my attendance is marked for the day or not. This is repetitive work so I decided to automate it. One solution is using Selenium. I know how to use Selenium as I worked with it. And this can be done in Selenium more easily as per me. But Selenium required a browser to open and there is a tedious work of setting up the environment properly. Surely I can do it all. But I want to open source it so that other students can also use it. So I decided to work with the requests library.
The requests library is amazing and simple to work with. So I refreshed my concept on HTTP methods and the requests library. I will briefly explain some of the methods of the requests.
For practice, I used the Wikipedia website. My task is to search “python” on Wikipedia. I worked on Jupyter Notebook. Here is a simple code and using the display power of Notebook.
First, we import libraries. We need only two libraries only for this task. The “requests” library for communicating using HTTP methods. HTML is used to execute an Html code. To display our results we use HTML.
In the above code, I have only used two HTTP methods to get and post but there are more than those. But these are frequently used and much need. We use get method when we are just reading the page and post when we are giving some value. The status_code is used to check where the operation was successful or not. There are various status codes. Basically 200–299 show OK status, 300–399 show redirection, 400–499 client-side errors, 500–599 server-side errors. To get the Html content of the page we use response.text and we passed that Html code to HTML function which executes it and displays the result then and there.
This is the result I got in my Jupyter Notebook. I am still working on my attendance monitor. I have got some success in that but I am stuck somewhere which I will sort out but It requires some more time. So this is it for now. It is great to work on this challenge. I encourage you to also challenge yourself and start working on some projects. Keep learning and keep smiling. :)