Untitled
unknown
plain_text
2 years ago
904 B
7
Indexable
import requests
def fetch_data_from_api(api_url):
try:
# Send a GET request to the API
response = requests.get(api_url)
# Check if the response is successful (status code 200)
response.raise_for_status()
# Assuming the API returns JSON data, parse it
data = response.json()
return data
except requests.exceptions.HTTPError as err:
print(f"HTTP error occurred: {err}")
except requests.exceptions.ConnectionError as err:
print(f"Connection error occurred: {err}")
except requests.exceptions.Timeout as err:
print(f"Timeout error occurred: {err}")
except requests.exceptions.RequestException as err:
print(f"An error occurred: {err}")
# Replace with the actual URL of your API
api_url = 'https://your-api-url.com/path'
# Fetch data from the API
data = fetch_data_from_api(api_url)
print(data)
Editor is loading...
Leave a Comment