Kalebu Jordan

Become a Pro Python Developer

Requests Library is  an elegant and simple HTTP library for Python, built for human beings, it basically use for sending HTTP request across web services such API. Requests Library doesn’t come with your standard library,  there for to install it  use pip command. 

On this tutorial you’re going to learn out the basics of Request library by applying it to get corona virus real time numbers . Let’s get started 

Installation

In Linux 
pip3 install requests
or
sudo apt-get install python3-requests
In Window
pip install requests

Basics of Requests Library

Making a request in Request is very simple, In order to make a successful request you might

First to specify the type of request you’re making whether its a (POST , GET , PUT , DELETE) and URL or Link you’re sending your requests , and additional parameters depending on the type of request

Let’s now make our very first request by to a github events, We will be making a get request (Get request works just like the way when you’re browser is requesting for a page) ,

The link to github events is https://api.github.com/events

Our Sample Code

>>> import requests
>>> link = 'https://api.github.com/events'
>>> response = requests.get(link)
>>> response.status_code
200
>>> response.json()[0]['actor']
{'avatar_url': 'https://avatars.githubusercontent.com/u/27856297?',
 'id': 27856297, 'login': 'dependabot-preview[bot]', 
 'url': 'https://api.github.com/users/dependabot-preview[bot]',
 'gravatar_id': '', 'display_login': 'dependabot-preview'}

Building Our Scraper (Sample Project)

As a sample project on learning requests, we are going to build a Simple Python Scripts for scraping information from a certain URL on real-time corona virus numbers .

Let’s get started

We will use the below URL to request covid-19 latest numbers of specific country .

scraping_url = "https://covidapi.info/api/v1/"

To get corona virus cases summary of a specific country we need twist a link to contain isocode of a specific country, isocode these short code 3-Letter code given to countries for Instance “KEN” for Kenya and etc

Sending Our get request to the server

Lets for example make get requests to get summary of corona number cases on Kenya, The isocode of Kenya is ‘KEN’.

scraping_url = "https://covidapi.info/api/v1/"+'country/'+'KEN'
response = requests.get(scraping_url)
print(response.json())
{'count': 91, 'result': {'2020-03-08': {'deaths': 0, 'confirmed': 0,
'recovered': 0}, '2020-03-15': {'deaths': 0, 'confirmed': 3, 'recovered': 0},....

The API return json response of corona virus statistics of a given country of each date since the first corona cases to the latest update information.

Mostly on scraping what we need is to get the latest statistics to do this we have to sort the json response and select the latest date statistics this can be done using OrderedDict and Sorted.

Building a Function to get isocodes of countries

But another challenge rises is that how do we get isocode of every country of which it’s information we wanna scrap, to simplify this I scraped json file of country with their respective isocode and stored them on a file and then I made a simple function which can return an isocode of any given country

def get_country_iso(country_name = None):
    try:
        if country_name:        
            with open('data.country') as file:
                content = file.read()
            data_iso = json.loads(content)
            country_name = get_close_matches(country_name, data_iso.keys())[0]
            isocode = data_iso.get(country_name, False)
            return isocode
        else:
            return
    except Exception as ex:
        print(ex)
        print('Error occured while scraping ..probably internet is not working')
        return

Example of usage of the get_country_iso

get_country_iso('Kenya')
'KEN'
get_country_iso('United Republic of Tanzania')
'TZA'

Building a Function to Automatically cases statistics

And then lastly to summarize I made a function which passes through the above procedure and is able to scrap latest corona virus cases of given country

def scrap_number_from_cloud():
    country_name = input('Enter country name : ')
    isocode = get_country_iso(country_name)
    if isocode:
        payload = {};headers= {}
        country_url = scraping_url+'country/'+isocode
        response = requests.request("GET", country_url, headers=headers, data = payload)
        info = response.json()['result']
        date  = list(OrderedDict(sorted(info.items())))[-1]
        data = info[date]
        message = "According to John Hopkins {} released on {} \n numbers are \nConfirmed cases {}\nDeath {} \nRecovered {}".format("https://coronavirus.jhu.edu/", date, data['confirmed'], data['deaths'], data['recovered'])
        print(message)
        return message
    else:
        response = 'Can\'t find match for your country'
        print(response)
        return response

Testing our Final Scraping script

The Final output we get by calling scrap_from_cloud function looks like this

>>scrap_number_from_cloud()
  Enter country name : Tanzania Republic of
  According to John Hopkins https://coronavirus.jhu.edu/
  released on 2020-04-21 numbers are
  Confirmed cases 254
  Death 10
  Recovered 11

The complete code is available on my GitHub profile CLICK HERE to view it, If you have any question you can just ask in a comment box and I will to you to the fast that I can

Don’t forget to subscribe to stay updated on upcoming Python tutorials

5 thoughts on “Learn basics of Requests Python

Leave a Reply

error

Enjoy this blog? Please spread the word :)

%d bloggers like this: