How to Load Weather Data Using Python?

This guide will explain how to fetch weather data from our Visual Crossing Weather API using Python. We will break down the code piece by piece, explain how to download the data, and provide an example of what an output looks like so you’re ready to integrate high-quality forecasting into any application or project. 

This script can access both historical and future weather, depending on the query date range you use. Our API automatically handles the transition between historical and future data, simplifying app development and integration. You do not have to change the query structure to utilize this code for either historical data or future forecasting. 

Why You Shouldn’t Scrape Weather Data

When developing an application or simply reviewing data, you may be tempted to manually or programmatically copy data from another web page. While this may seem like a good shortcut, it can actually cause you more headaches in the long run.

If the source page changes, your script will no longer work. Additionally, most websites explicitly ban data scraping under their terms of service. 

A dedicated forecasting API like Visual Crossing is a safer solution, as it ensures data accuracy and reliability. You can use scripts to automatically fetch weather data that has already been compiled and perfected for your use.

Visual Crossing is a secure and scalable solution – plus, it’s free to sign up. We provide everything you need to create an attractive weather app or access historical data from around the world. We also offer installation documentation and FAQs to get you up and running faster. 

Skip to the source code.

Prerequisites

To start, sign up for a free account at Visual Crossing Weather Services. Our perpetual free tier allows up to 1000 import requests per day without any cost. If you need to query more weather data, you can pay per result or sign up for a monthly plan.

When writing Python Scripts, you may choose to use VS Code. Depending on your specific needs, you can install the software or use the browser program. VS Code works on Windows, OS, and Linux, and it supports a variety of programming languages, including Python. IDLE, while it is the native integrated development environment for Python, does not have many of the plugins you may need, such as debugging and syntax highlighting. 

Our Python script was written in Python 3.8.2, but it should work in most recent versions. To keep things simple, we have kept the Python library requirements to a minimum. In this sample, we are going to use the CSV format to download the data file, so we include a library to help process the data returned by the API.

Here’s the full list of import statements in our script

import csv
import codecs
import urllib.request
import urllib.error
import sys

Setting Up the Weather Data Input Parameters

The first part of our script sets up the parameters to download the weather data, which will modify the base URL.

BaseURL = 
'
https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/'

The script includes the following sections:

API Key: This is your personal key to our database, informing our servers that you have permissi

on to fetch data. To work, all HTTP requests must include this key.

ApiKey='YOUR_API_KEY'

Units: You can choose between US units, like Fahrenheit, or metric, like Celsius.

#UnitGroup sets the units of the output - us or metric
UnitGroup='us'

Location: We provide weather forecast information for anywhere in the world, so you must specify what city and country you’d like to view. For example, you may pick London, Ontario, Canada, or London, United Kingdom. You may pull for multiple cities, and you can also use latitude and longitude values for more precise data.

#Location for the weather data
Location='Washington, DC'

Start and End Dates: If you do not specify a date, the API will default to the current weather and a 15-day forecast. You can use the same script regardless of whether you want historical, current, or future weather. You can also use a dynamic date period as the start date, such as yesterday, tomorrow, last 7 days, and so on. See Weather Data Periods for more details about the options available. The code requests a start and end date in the form YYYY-MM-DD. The format of the date is important for the weather API query.

#Optional start and end dates
#If nothing is specified, the forecast is retrieved.
#If start date only is specified, a single historical or forecast day will be retrieved
#If both start and end date are specified, a date range will be retrieved
StartDate = ''
EndDate=''

Content Type: This part tells the system how to download your information. You can choose between JSON and CSV.

#JSON or CSV 
#JSON format supports daily, hourly, current conditions, weather alerts and events in a single JSON package
#CSV format requires an 'include' parameter below to indicate which table section is required
ContentType="csv"

Data Sections: You can select different data sections depending on what you need, whether it’s just the day’s forecast or weather alerts. 

#include sections
#values include days,hours,current,alerts
Include="days"

While our example uses hardcoded examples, developers can use a number of other parameters and create more dynamic functions. The Weather API documentation provides more information on the full set of Weather API parameters.

Downloading the weather data

The next section of code creates the Weather API request from the parameters, submits the request to the server, and then parses the result.

The data is retrieved using a RESTful weather API, so we simply have to create a web query URL within the Python script and run it as a web query to retrieve the result data.

This example uses a basic GET request, but developers can also choose to use POST or other formats depending on their specific needs. You could also use ODATA for specialized data import and data science applications. See the Weather API documentation section for more information.

Our example provides a clean and functional example that will work for any CSV response.

#basic query including location
ApiQuery=BaseURL + Location
#append the start and end date if present
if (len(StartDate)):
   ApiQuery+="/"+StartDate
   if (len(EndDate)):
       ApiQuery+="/"+EndDate
#Url is completed. Now add query parameters (could be passed as GET or POST)
ApiQuery+="?"
#append each parameter as necessary
if (len(UnitGroup)):
   ApiQuery+="&unitGroup="+UnitGroup
if (len(ContentType)):
   ApiQuery+="&contentType="+ContentType
if (len(Include)):
   ApiQuery+="&include="+Include
ApiQuery+="&key="+ApiKey
print(' - Running query URL: ', ApiQuery)
print()
try:
   CSVBytes = urllib.request.urlopen(ApiQuery)
except urllib.error.HTTPError  as e:
   ErrorInfo= e.read().decode() 
   print('Error code: ', e.code, ErrorInfo)
   sys.exit()
except  urllib.error.URLError as e:
   ErrorInfo= e.read().decode() 
   print('Error code: ', e.code,ErrorInfo)
   sys.exit()

The final two lines of this code section download the requested weather data and provides some simple error handling. In this example, we have used the urllib.request library to provide the retrieval functionality.

Error handling with urllib.request

Error handling is crucial for any application that requests library data from an external source. Our example uses try-except blocks to handle HTTP and URL errors. If you get an error, you should log or display the response body to understand and troubleshoot failed queries.

Testing the query directly in a browser is a good way to debug or review the data structure and workshop solutions. You can also use the Weather Data Services query builder page to construct requests and see results.

Using the weather data

After the data is retrieved, it is parsed as Comma-Separated Values (CSV). You can then print, analyze, store, or apply the data in your downstream workflows as necessary.

# Parse the results as CSV
CSVText = csv.reader(codecs.iterdecode(CSVBytes, 'utf-8'))

The raw data is a table of weather data rows. In this case, the historical weather data for each day is requested.

name,datetime,tempmax,tempmin,temp,feelslikemax,feelslikemin,feelslike,dew,humidity,precip,precipprob,precipcover,preciptype,snow,snowdepth,windgust,windspeed,winddir,sealevelpressure,cloudcover,visibility,solarradiation,solarenergy,uvindex,severerisk,sunrise,sunset,moonphase,conditions,description,icon,stations
"Washington, DC, United States",2021-12-14,58.9,34.8,44.3,58.9,34.8,44,28.4,58.5,0,0,,,0,0,9.2,4.7,66.6,1032.2,34.4,12.5,118,7.1,4,10,2021-12-14T07:19:08,2021-12-14T16:46:50,0.41,Partially cloudy,Partly cloudy throughout the day.,partly-cloudy-day,"KDCA,F0198,KADW,KDAA,PWDM2"
"Washington, DC, United States",2021-12-15,54.1,36,45.3,54.1,36,45.1,39,78.9,0,4,,,0,0,8.5,4.3,109.6,1031.5,63.5,15,81.4,7.1,4,10,2021-12-15T07:19:49,2021-12-15T16:47:07,0.44,Partially cloudy,Partly cloudy throughout the day.,partly-cloudy-day,
"Washington, DC, United States",2021-12-16,62,45,53.7,62,41.9,52.6,36.1,67.1,0,16,,,0,0,20.8,10.3,208.8,1019.9,55.7,15,73.3,6.4,3,10,2021-12-16T07:20:29,2021-12-16T16:47:26,0.47,Partially cloudy,Partly cloudy throughout the day.,partly-cloudy-day,Address,Date time,Minimum Temperature,Maximum Temperature,Temperature,Dew Point,Relative Humidity,Heat Index,Wind Speed,Wind Gust,Wind Direction,Wind Chill,Precipitation,Precipitation Cover,Snow Depth,Visibility,Cloud Cover,Sea Level Pressure,Weather Type,Latitude,Longitude,Resolved Address,Name,Info,Conditions
"Herndon,VA",01/01/2019,44.6,60.8,53.1,45.1,75.58,,23.2,36.3,269.29,41.4,0,8.33,,9.1,83.5,1014.8,"Mist, Fog, Light Rain",38.96972,-77.38519,"Herndon,VA","","","Overcast"
"Herndon,VA",01/02/2019,37.3,44.9,42.4,35.9,78.17,,8.2,,143.95,38.7,0,0,,10,98.3,1023.6,"",38.96972,-77.38519,"Herndon,VA","","","Overcast"

The above example is just the direct output, but it can then be used for databases, dashboards, and data science pipelines. For example, you can analyze the weather data in R and then load it into a database

We can now use another code to review the rows of CSV data and print them in a more user-friendly format. 

RowIndex = 0
# The first row contains the headers and the additional rows each contain the weather metrics for a single day
# To simplify our code, we use the knowledge that column 0 contains the location and column 1 contains the date.  The data starts at column 4
for Row in CSVText:
   if RowIndex == 0:
       FirstRow = Row
   else:
       print('Weather in ', Row[0], ' on ', Row[1])
       ColIndex = 0
       for Col in Row:
           if ColIndex >= 4:
               print('   ', FirstRow[ColIndex], ' = ', Row[ColIndex])
           ColIndex += 1
   RowIndex += 1

CSV is a flexible format that can be used for nearly any application, whether that is a weather app or a scientific study. However, you can also utilize JSON if you already have tools optimized for that format. 

Parsing JSON data

If you prefer to use JSON-formatted data (which can often be a significantly faster way of processing the data if you are processing the data within Python), you can parse the response as follows:

import json
....
weatherData = json.loads(data.decode('utf-8'))

The variable weatherData now contains the parsed weather data set and can be easily processed.

Full Source Code for Python Weather Data Downloads

Our GitHub page has the full code listing for this, where you can review references and examples of use cases. 

Full source code.

Explore additional Python and Java options using our API. We also have a tutorial on How to Import Weather Data into MySQL

Next steps

In conclusion, fetching historical and future weather forecasts is simple with our Python code. It also avoids the hassles of data scraping, which can lead to broken links and irretrievable data.

A free API key from Visual Crossing enables you to build projects, visualize data, or analyze weather trends, all with the most advanced weather information from any country in the world. We encourage you to explore the many use cases for high-quality weather data, whether you are a citizen scientist or climatologist. 

Questions or need help?

If you have a question or need help, please post on our actively monitored forum for the fastest replies. You can also contact our Support Team.

Scroll to Top