How to Extract Weather Data from Google in Python? (original) (raw)

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, and also we will see another method to fetch a schematic depiction of a location's weather data for the next two days in Python without utilizing an API.

Method 1:

**Module needed:

**Requests: Requests allow you to send HTTP/1.1 requests extremely easily. This module also does not come built-in with Python. To install this type the below command in the terminal.

!pip install requests
!pip install beautifulsoup4

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

"https://www.google.com/search?q="+"weather"+{cityname}

**Step-wise implementation of code:

Step 1: Import the requests and bs4 library

Python `

importing the library

import requests from bs4 import BeautifulSoup

`

**Step 2: Create a URL with the entered city name in it and pass it to the get function.

Python `

enter city name

city = "lucknow"

create url

url = "https://www.google.com/search?q="+"weather"+city

requests instance

html = requests.get(url).content

getting raw data

soup = 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 the find function and passing the tag name and class name.

13°C
Saturday 11:10 am

Python `

get the temperature

temp = soup.find('div', attrs={'class': 'BNeawe iBp4i AP7Wnd'}).text

this contains time and sky description

str = soup.find('div', attrs={'class': 'BNeawe tAd8D AP7Wnd'}).text

format the data

data = 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.

Python `

list having all div tags having particular class name

listdiv = soup.findAll('div', attrs={'class': 'BNeawe s3v9rd AP7Wnd'})

particular list with required data

strd = listdiv[5].text

formatting the string

pos = strd.find('Wind') other_data = strd[pos:]

`

**Step 5: Printing all the data

Python `

printing all the data

print("Temperature is", temp) print("Time: ", time) print("Sky Description: ", sky) print(other_data)

`

**Python Code

Python `

Importing necessary libraries

import requests from bs4 import BeautifulSoup

Enter city name

city = "lucknow"

Creating URL and making requests instance

url = "https://www.google.com/search?q=" + "weather" + city html = requests.get(url).content

Getting raw data using BeautifulSoup

soup = BeautifulSoup(html, 'html.parser')

Extracting the temperature

temp = soup.find('div', attrs={'class': 'BNeawe iBp4i AP7Wnd'}).text

Extracting the time and sky description

str_ = soup.find('div', attrs={'class': 'BNeawe tAd8D AP7Wnd'}).text data = str_.split('\n') time = data[0] sky = data[1]

Getting all div tags with the specific class name

listdiv = soup.findAll('div', attrs={'class': 'BNeawe s3v9rd AP7Wnd'})

Extracting other required data

strd = listdiv[5].text pos = strd.find('Wind') other_data = strd[pos:]

Printing the extracted weather data

print("Temperature is:", temp) print("Time:", time) print("Sky Description:", sky) print(other_data)

`

**Output:

Method 2: Getting Weather Data Without Scraping

Module needed:

**Requests: Requests allow you to send HTTP/1.1 requests extremely easily. The HTTP request returns a response object with all of the required response data. This module also does not come built-in with Python. To install this type the below command in the terminal.

pip install requests

Approach:

Below is the implementation:

Python `

Importing the requests module

import requests

Sending request to get the IP location information

res = requests.get('https://ipinfo.io/') data = res.json() # Receiving the response in JSON format

Extracting the location of the city from the response

citydata = data['city'] print("Current Location:", citydata)

Passing the city name to the URL to get weather data

url = 'https://wttr.in/%7B%7D'.format(citydata) res = requests.get(url)

Printing the schematic weather details of the city

print(res.text)

`

Output:

Screenshot

**Get the complete notebook link: click here.