Making a simple request to APIs in python
- 2023年2月14日
- 技術情報
Today, I would like to share about making a API request using ‘requests’ library in Python. Let’s take a look.
First of all, install the ‘requests’ library by the following command.
pip install requests
or
pip3 install requests
Here is the code to make a GET request to a public API.
import requests
api = 'https://catfact.ninja/fact'
response_data = requests.get(api)
response_data_json = response_data.json()
print(response_data_json)
The result is as follows.
{'fact': 'Lions are the only cats that live in groups, called prides. Every female within the pride is usually related.', 'length': 109}
This is the example request to a public API. You can learn more HERE for advanced usage.
Hope you enjoy that.
By Asahi
waithaw at 2023年02月14日 10:00:00