Run API Call Using Python

Published: 17 March 2024
on channel: InfoTech Essence
144
10

Run API Call Using Python #pythontutorial
Code for Practice
import requests
import pandas as pd

Replace 'YOUR_USERNAME' and 'YOUR_PASSWORD' with your actual credentials
username = 'XXXXXXXXXXXXX'
password = 'Pblu496F9Z'
url = 'https://api.meteomatics.com/2024-02-2...

Sending a GET request with basic authentication
response = requests.get(url, auth=(username, password))

if response.status_code == 200:
try:
data = response.json()
print(data) # Print the JSON data to inspect

Check if the response is empty or not in the expected JSON format
if not data:
print("Empty response or not in JSON format.")
else:
Assuming data is a list of dictionaries, you may need to adapt this based on the API response format
df = pd.DataFrame(data)

Specify the path where you want to save the Excel file
excel_file_path = "output_data.xlsx"

Save the DataFrame to Excel
df.to_excel(excel_file_path, index=False)

print(f"Data saved to {excel_file_path}")
except requests.exceptions.JSONDecodeError as e:
print(f"Error decoding JSON: {e}")
else:
print(f"Error: {response.status_code}")
print(response.text) # Print the response content for inspection


On this page of the site you can watch the video online Run API Call Using Python with a duration of hours minute second in good quality, which was uploaded by the user InfoTech Essence 17 March 2024, share the link with friends and acquaintances, this video has already been watched 144 times on youtube and it was liked by 10 viewers. Enjoy your viewing!