How to Fetch Live Weather Data Using Python

Python’s simplicity and adaptability have helped it gain popularity throughout the years. you may 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’re able to use this fetched data to develop a simple weather application.

4

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 libraryto 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:

Weather Forecast of cities in a map

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

The code used in this project is available in aGitHub repositoryand is free for you to use under the MIT license.

chrome screenshot displaying New Delhi weather data scraping

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:

Create a function,find_weather(), to make a query to Google using therequests.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:

New Delhi weather data scraping using python

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:

Next, pass these IDs to theselect()method. This method runs a CSS selector against the parsed document and returns all the matching elements. ThegetText()method extracts the text from the HTML element. Thestrip()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.

OpenWeatherMap My API keys website screenshot

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

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.

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, considermoving the API key data to a .env filefor enhanced security.

Go toOpenWeatherMap’swebsite and create a free account. After creating the account, you can find your API keys on theMy API Keyspage. 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.

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

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 thejson()method to perform further operations. If the city is found, you will have to resolve the dict object (res) to extract the required information.

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.

You know the basics and now you’re ready to apply them. Get started with these Python projects!

My foolproof plan is to use Windows 10 until 2030, with the latest security updates.

When your rival has to bail out your assistant.

Flagship price, mid-range phone.

So much time invested, and for what?

If an AI can roast you, it can also prep you for emergencies.

Technology Explained

PC & Mobile