DEV Community

loading...

Scraping Weather Data with Python and Beautiful Soup

proxiesapi profile image Mohan Ganesan Originally published at proxiesapi.com ・2 min read

Today we are going to see how we can scrape Weather data using Python and BeautifulSoup in a simple and elegant manner.

The aim of this article is to get you started on a real-world problem solving while keeping it super simple so you get familiar and get practical results as fast as possible.

So the first thing we need is to make sure we have Python 3 installed. If not, you can just get Python 3 and get it installed before you proceed.

Then you can install beautiful soup with:
We will also need the libraries requests, lxml and soupsieve to fetch data, break it down to XML, and to use CSS selectors. Install them using...

Once installed open an editor and type in...
Now let's go to the Weather.com 10 day forecast page and inspect the data we can get.

This is how it looks:
Back to our code now. Let's try and get this data by pretending we are a browser like this.
Save this as weather_bs.py.

If you run it.
You will see the whole HTML page.

Now let's use CSS selectors to get to the data we want. To do that let's go back to Chrome and open the inspect tool.
We notice that all the individual rows of data are contained in a with the class 'clickable closed'. We can get BeautifulSoup to select that data like this.
This print all the content in each of the rows.

We now can pick out classes inside these rows that contain the data we want. The date is in the day-detail class.
If you run it will print out all the days.
Bingo!! we got the days.

Now with the same process, we get the other data like weather description, temperature, wind speed, precipitation etc.
We even added a separator to show where each day ends. You can now pass this data into an array or save it to CSV and do whatever you want.

If you want to use this in production and want to scale to thousands of links then you will find that you will get IP blocked easily by Weather.com. In this scenario using a rotating proxy service to rotate IPs is almost a must.

Otherwise, you tend to get IP blocked a lot by automatic location, usage, and bot detection algorithms.

Our rotating proxy server Proxies API provides a simple API that can solve all IP Blocking problems instantly.

With millions of high speed rotating proxies located all over the world,
With our automatic IP rotation
With our automatic User-Agent-String rotation (which simulates requests from different, valid web browsers and web browser versions)
With our automatic CAPTCHA solving technology,
Hundreds of our customers have successfully solved the headache of IP blocks with a simple API.

The whole thing can be accessed by a simple API like below in any programming language.

Discussion (0)

pic
Editor guide