How to Extract Weather Data from Google in Python?

21 Jun, 2021
In this article, we will see how to extract weather data from google. Google does not have its own weather API, it fetches data from weather.com and shows it when you search on Google. So, we will scrape the data from Google.
Module needed:
Requests: Requests allows you to send HTTP/1.1 requests extremely easily. This module also does not comes built-in with Python. To install this type the below command in the terminal.
pip install requests
bs4: Beautiful Soup is a library that makes it easy to scrape information from web pages. Whether it be an HTML or XML page, that can later be used for iterating, searching, and modifying the data within it.
Approach:
Import the module
Enter the city name with the URL
"https://www.google.com/search?q="+"weather"+{cityname}
Make requests instance and pass the URL
Get the raw data.
Extract the required data from the soup.
Finally, print the required data.
Step-wise implementation of code:
Step 1: Import the requests and bs4 library
Python3
# importing the libraryimport requestsfrom bs4 import BeautifulSoup
Step 2: Create a URL with the entered city name in it and pass it to the get function.
Python3
# enter city namecity = "lucknow" # create urlurl = "https://www.google.com/search?q="+"weather"+city # requests instancehtml = requests.get(url).content # getting raw datasoup = BeautifulSoup(html, 'html.parser')
Step 3: Soup will return a heap of data with HTML tags. So, a chunk of data has been shown below from which we will get all the necessary data with the help of find function and passing the tag name and class name.
<div class=”kvKEAb”><div><div><div class=”BNeawe iBp4i AP7Wnd”><div><div class=”BNeawe iBp4i AP7Wnd”>13°C</div></div></div></div></div><div><div><div class=”BNeawe tAd8D AP7Wnd”> <div><div class=”BNeawe tAd8D AP7Wnd”>Saturday 11:10 am
Python3
# get the temperaturetemp = soup.find('div', attrs={'class': 'BNeawe iBp4i AP7Wnd'}).text # this contains time and sky descriptionstr = soup.find('div', attrs={'class': 'BNeawe tAd8D AP7Wnd'}).text # format the datadata = str.split('\n')time = data[0]sky = data[1]
Step 4: Here list1 contains all the div tags with a particular class name and index 5 of this list has all other required data.
Python3
# list having all div tags having particular clas snamelistdiv = soup.findAll('div', attrs={'class': 'BNeawe s3v9rd AP7Wnd'}) # particular list with required datastrd = listdiv[5].text # formatting the stringpos = strd.find('Wind')other_data = strd[pos:]
Step 5: Printing all the data
Python3
# printing all the dataprint("Temperature is", temp)print("Time: ", time)print("Sky Description: ", sky)print(other_data)
Output:
Below is the full implementation:
Python3
# importing libraryimport requestsfrom bs4 import BeautifulSoup # enter city namecity = "lucknow" # creating url and requests instanceurl = "https://www.google.com/search?q="+"weather"+cityhtml = requests.get(url).content # getting raw datasoup = BeautifulSoup(html, 'html.parser')temp = soup.find('div', attrs={'class': 'BNeawe iBp4i AP7Wnd'}).textstr = soup.find('div', attrs={'class': 'BNeawe tAd8D AP7Wnd'}).text # formatting datadata = str.split('\n')time = data[0]sky = data[1] # getting all div taglistdiv = soup.findAll('div', attrs={'class': 'BNeawe s3v9rd AP7Wnd'})strd = listdiv[5].text # getting other required datapos = strd.find('Wind')other_data = strd[pos:] # printing all dataprint("Temperature is", temp)print("Time: ", time)print("Sky Description: ", sky)print(other_data)
Output:
gabaa406
Picked
Python BeautifulSoup
Python-requests
Technical Scripter 2020
Web-scraping
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.