Run API Call Using Python

Publicado em: 17 Março 2024
no canal de: 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


Nesta página do site você pode assistir ao vídeo on-line Run API Call Using Python duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário InfoTech Essence 17 Março 2024, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 144 vezes e gostou 10 espectadores. Boa visualização!