53: Getting Data from APIs
In this exercise you'll access the Application Programmer Interface (API) I use for my learncodethehardway.com website. In web development an API is usually a combination of the following:
- A web server that you can access with the HTTP protocol. You used HTTP when you used urllib to get the beer production PDFs from the ttb.gov website. HTTP is also what your browser uses to display the web application to you.
- This web server responds in some data format that's easily parsed. This is what differentiates a PDF from an API. Sure, you're getting data on beer production from ttb.gov but you have to parse that data out of a PDF. An API gives you the data ready to go in a format that loads directly into your application with no manual parsing.
- A higher level API will provide features to discover how the API works automatically. This is a more advanced feature of APIs but many of them will have an initial URL that describes the API, and then each piece of data will describe what's allowed and link to related elements. There is no official standard on how this is done, but if it's available it's nice to have.
I use an API in my web application that conforms to #1 and #2, but only partially #3 since I don't actually care if other people can dynamically figure out how to use my API. #3 on this list is a common practice since private APIs are made for a specific application written by the API owners, while public APIs are intended for anyone to use and discover. I chose my private API because many times those are the most useful because other people are too lazy to reverse engineer them.
Register for Learn Python the Hard Way, 5th Edition (2023-2024)
Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.