×We're offering Women's Scholarships for applicants to our February class! Details here.
By the end of this chapter, you should be able to:
requestsmodule to perform server side requests
To issue server side requests we use the
requests module, which you can install using
pip3 install requests. You can read more about it here.
Let's start with a very simple example to get data from the OMDB API.
import requests # Make an HTTP GET request to a specific URL r = requests.get('http://omdbapi.com?t=titanic') # returns a response object r.status_code # 200 r.ok # True r.headers # see a dictionary of HTTP headers r.json() # Examine what this data looks like in JSON
We can also use the requests module to make a POST request:
import requests payload = dict(id=1) r = requests.post('some_url', params=payload) r.text # see your params in an args dictionary
The requests module is very useful for sending and retrieving data from an API and can also be used to authenticate users (logging in through Facebook / Twitter / etc.).
When you're ready, move on to Web Scraping Exercises