Making HTTP Requests with Python-Requests
Python includes a built-in HTTP client, urllib2, but its interface is clunky and hard to use. There exists a much better and easier to use HTTP client interface called Python Requests. Requests is not included with Python by default, so we must install it.
Installing Requests
Making a GET request
The simplest use of this library is making an HTTP GET Request. More flexibility can be added in the form of query parameters, authentication, custom request headers, cookies, and more. Requests also provides a nicely formatted response object, complete with HTTP Status code (that error 404 everyone loves).
At its simplest, Requests is used as:
To add on a query parameter, include a 'params' object in the request
To add on a header, include a 'headers' object in the request
If you are accessing a website that needs authentication (Basic, Digest, OAuth1), Requests can handle it as well. To use authentication, include an 'auth' object in the request. The authentication defaults to HTTP Basic (username/password in the clear) if one is not specified.
Creating a Forecast.IO account
This service is free up to 10k requests per day. No billing info is needed to sign up.
- Go to http://https://developer.forecast.io/ and click the "Register" button in the top right corner.
- Make note of your API Key on the bottom of the screen
Requesting forecast.io data
Use Python-requests to get forecast data for a given location and then print out tomorrow's weather