live data from an api in python

To retrieve live data from an API in Python, you can use the requests module. The following example demonstrates how to fetch data from an API:

main.py
import requests

url = "https://dummyapi.io/data/api/user"

headers = {
    "app-id": "<your app id>"
}

response = requests.get(url, headers=headers)

if response.status_code == 200:
    data = response.json()
    print(data)
else:
    print(f"Error fetching data. Status code: {response.status_code}")
304 chars
16 lines

In this example, we're making a GET request to the specified API endpoint with the app-id header. The response is then parsed as JSON and returned as a Python dictionary.

You'll need to replace the url and app-id values with the appropriate values for the API you're trying to access.

related categories

gistlibby LogSnag