Using Python to retrieve data from APIs

The first step is to import the requests module. I will also import the json module, which can be used to output the data retrieved from the API to a JSON file. In the beginning of get_recent_issues.py, the following code imports the requests and json modules:

import requests
import json

The next step is to create a string called base URL. The base URL is the beginning part of the URL, and can be followed by additional parameters. For now, the base URL is all you need, but in the next section you will build on it in order get data iteratively. In the following continuation of get_recent_issues.py, a string is created containing the base URL for the issues resource of the Seeclickfix API:

import requests
import json

## build a base url which is the endpoint to the api
## for this script, this is all you need
base_url = "https://seeclickfix.com/api/v2/issues?"

The next step is to replicate the action of the browser. In other words, you will need to submit a get request to the URL and store the response. This can be done using the requests.get() function of the requests module, which takes as an argument a URL string and returns a response object containing the information in the response from the server. In the following continuation of get_recent_issues.py, the requests module is used to submit a get request to the base URL:

....
base_url = "https://seeclickfix.com/api/v2/issues?"

## submit a get request to the URL and
## collect the response in the response object
response = requests.get(base_url)

An http response contains additional information besides the main content. The requests module parses the response into its various components. Because the body of this particular response is expected to be in JSON format, you can use the response.json() function of a response object to both extract the JSON text, and parse the text into a Python data structure. 

At this point, it may be worth doing a bit of exploration of the data in the response, though I will simply save the result to a file for now. In the following continuation of get_recent_issues.py, the response.json() method is used to extract the response body, which is then saved to a JSON file:

response = requests.get(base_url)

## use the json module to write the response data
## from the api into a json file
fout = open("output_data/scf_recent_issues.json","w")
json.dump(response.json(),fout,indent=4)
fout.close()

Running get_recent_issues.py should now produce an output JSON file containing the 10 most recent issues reported on the Seeclickfix platform:

This covers how to get data within Python, but it does not take advantage of the programming interface that Python offers. In the next section, I will demonstrate the use of URL parameters to retrieve larger and more specific collections of data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset