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
На этой странице сайта вы можете посмотреть видео онлайн Run API Call Using Python длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь InfoTech Essence 17 Март 2024, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 144 раз и оно понравилось 10 зрителям. Приятного просмотра!