MakeUseOf logo

How to Fetch Live Weather Data Using Python

Weather Forecast of cities in a map
No attribution required -- Unsplash
https://unsplash.com/photos/kKyxIwvljBg

Yuvraj is a passionate technical writer with a computer science degree from the esteemed University of Delhi, India.

His deep understanding and expertise in programming, software development, artificial intelligence, and blockchain have driven his passion for writing on cutting-edge technology. Since 2019, he has been a technical writer, sharing his knowledge on various web development technologies. Yuvraj's stint as a developer for several startups complements his writing skills.

In addition to his professional pursuits, Yuvraj enjoys playing chess and has contributed to prominent publications, including GeeksforGeeks.

Sign in to your MakeUseOf account

Python's simplicity and adaptability have helped it gain popularity throughout the years. You can easily retrieve useful data over the internet with Python. You can then use that data to drive a practical application.

Learn how to find real-time weather data using web scraping and APIs. You can use this fetched data to develop a simple weather application.

Get Current Weather Details of a City Using Web Scraping

Web scraping is the process of extracting data and content from a website. Autonomously fetching data from the web opens up a lot of use cases. But most of this data is in HTML format, which you need to parse and inspect to extract relevant data.

You can extract live weather data of any city using web scraping. Python's BeautifulSoup library is the go-to library to pull data out of HTML and XML files. You need to install the BeautifulSoup Python library via pip to begin the scraping process. Run the following command in the terminal to install the BeautifulSoup and requests libraries:

pip install beautifulsoup4 requests

After you’ve installed the required libraries, start by importing them in your code:

The code used in this project is available in a GitHub repository and is free for you to use under the MIT license.

from bs4 import BeautifulSoup
import requests

Next, you need to provide the header details so that the client and the server can pass additional information with an HTTP request or response:

headers = {
 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}

Create a function, find_weather(), to make a query to Google using the requests.get() method. You’ll use a search URL to get a city's weather, then scrape the meaningful data to get location, temperature, time, and weather description. Then, use BeautifulSoup to parse the received HTML response:

def find_weather(city_name):
 city_name = city_name.replace(" ", "+")
 
 try:
 res = requests.get(
 f'https://www.google.com/search?q={city_name}&oq={city_name}&aqs=chrome.0.35i39l2j0l4j46j69i60.6128j1j7&sourceid=chrome&ie=UTF-8', headers=headers)
 
 print("Loading...")
 
 soup = BeautifulSoup(res.text, 'html.parser')
 location = soup.select('#wob_loc')[0].getText().strip()
 time = soup.select('#wob_dts')[0].getText().strip()
 info = soup.select('#wob_dc')[0].getText().strip()
 temperature = soup.select('#wob_tm')[0].getText().strip()
 
 print("Location: " + location)
 print("Temperature: " + temperature + "°C")
 print("Time: " + time)
 print("Weather Description: " + info)
 except:
 print("Please enter a valid city name")

To extract the element IDs, carry out a Google search and inspect the page in your browser using web tools. You need to inspect the element to find the IDs of the HTML element for which you want to extract data:

[画像:chrome screenshot displaying New Delhi weather data scraping]
No attribution required -- Author screenshot (Yuvraj Chandra)

Next, pass these IDs to the select() method. This method runs a CSS selector against the parsed document and returns all the matching elements. The getText() method extracts the text from the HTML element. The strip() method removes any leading and trailing whitespace characters from the text. Once you’ve extracted a clean value, you can store it in a variable.

Finally, ask the user to input a city and pass it to the find_weather function:

city_name = input("Enter City Name: ")
city_name = city_name + " weather"
find_weather(city_name)

Now, when you run the code, it will prompt you to enter a city name. You must enter a valid city name to get the results or the code will raise an exception.

[画像:New Delhi weather data scraping using python]
No attribution required -- Author Screenshot (Yuvraj Chandra)

Get Current Weather Details of a City Using OpenWeatherMap API

OpenWeatherMap is an online service, owned by OpenWeather Ltd. Its API provides global weather data including current weather, forecasts, and past data for any location. The free tier of the OpenWeatherMap API provides current weather data with a limit of 60 calls/minute. You need to create an account on OpenWeatherMap to get your own API key.

Do not push the code with the API key to a public repository as anyone with access to your source files can see and steal your key. In a production app, consider moving the API key data to a .env file for enhanced security.

Go to OpenWeatherMap's website and create a free account. After creating the account, you can find your API keys on the My API Keys page. You can use the default API key provided by the OpenWeatherMap or generate one of your own. OpenWeatherMap provides the support to generate as many API keys as needed for your projects.

[画像:OpenWeatherMap My API keys website screenshot]
No attribution required -- Author screenshot (Yuvraj Chandra)

Now, you're ready to retrieve the live weather data.

# Importing libraries
import requests
import json
 
# Enter your OpenWeatherMap API key here
# DO NOT push it to a public repository
API_Key = "Your_API_Key"
 
# Provide a valid city name
city_name = input("Enter city name: ")
 
# Constructing the API URL path
url = f"http://api.openweathermap.org/data/2.5/weather?q={city_name}&appid={API_Key}"
 
# Making a get request to the API
response = requests.get(url)
 
# Converting JSON response to a dictionary
res = response.json()
 
# Uncomment the next line to see all
# data that are fetched from the API
# print(res)
 
# Checking if the city is found
# If the value of "cod" is not 404,
# that means the city is found
if res["cod"] != "404":
 data = res["main"]
 
 # Storing the live temperature data
 live_temperature = data["temp"]
 
 # Storing the live pressure data
 live_pressure = data["pressure"]
 desc = res["weather"]
 
 # Storing the weather description
 weather_description = desc[0]["description"]
 print("Temperature (in Kelvin scale): " + str(live_temperature))
 print("Pressure: " + str(live_pressure))
 print("Description: " + str(weather_description))
 
else:
 # If the city is not found,
 # this block of code will be executed
 print("Please enter a valid city name")

If you provide a valid API key and enter the correct city name, you'll receive the data from the API in JSON format. Next, you need to convert this JSON format data into a Python object using the json() method to perform further operations. If the city is found, you will have to resolve the dict object (res) to extract the required information.

[画像:New Delhi weather data from OpenWeatherMap API]
No attribution required -- Author screenshot (Yuvraj Chandra)

Develop Weather Application Using the Live Weather Data

Now that you've learned how to fetch live data using the OpenWeatherMap API, you're ready to develop a simple weather application using it. Building a weather application can help you to apply what you know and hone your Python skills.

Getting your hands dirty on practical projects can make you a better developer. You can develop some other Python projects like a login system, quiz app, or URL shortener to solidify your Python development skills.

AltStyle によって変換されたページ (->オリジナル) /