Requests is a powerful and easy to use HTTP library for Python
Need to get HTML data? Need to interact with an API? Requests can do that.
While Python has a built-in library urllib3 for these situations, requests wraps this library with an easy to use interface.
To install requests, you can use pip to install it.
pip install -U requests
The -U is a good practice to force an upgrade if it is already installed.
I recommend using a virtual environment. You can learn more about them in my previous tutorial on Virtual Environments.
While requests has a lot of functionality, I am going to cover the basic usage and you can refer to the documentation for the more advanced usage.
import requests req = requests.get('https://pokeapi.co/api/v2/pokemon/ditto/`)
You can verify you have a proper response by checking the status code.
200 means everything went well, you can look up other codes here.
You can also use this technique to verify a good status code.
if req.status_code == requests.codes.ok:
There is a cleaner way to send a request and check the status code:
import requests req = req = requests.get('https://pokeapi.co/api/v2/pokemon/ditto/`) try: req.raise_for_status() except requests.exceptions.HTTPError as e: print e
You can retrieve the response data using
req.json() as bytes with
req.content, or raw with
Most of the time you will be using
req.json() and will be able to use the result as normal json data.
Requests can be used to POST, pass parameters, work with custom headers, and work with form data. If you want to learn other uses of the requests library, check out their documentation.
Requests is a simple by powerful python module that will handle 98% of your HTTP needs.
My Python Tips Series
- f-strings in Python 3.6
- Underscores in numeric literals
- A better interactive shell
- Secrets Module - New in 3.6
- PEP 8
- Named Tuples
- Type Annotation
- Jupyter Notebooks
- Getting Help
- Virtual Environments
- Expiring Dict
- DRY Programming
- Knowing what exists
- Caching data with CacheTools
- Walrus Operator