Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Beautiful Soup
  • Selenium
  • Scrapy
  • urllib
  • Request
  • open cv
  • Data analysis
  • Machine learning
  • NLP
  • Deep learning
  • Data Science
  • Interview question
  • ML math
  • ML Projects
  • ML interview
  • DL interview
Open In App
Next Article:
How to Use Google Cloud Function with Python ?
Next article icon

How to Extract Weather Data from Google in Python?

Last Updated : 11 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  • 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

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.

<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

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:

  • Import the requests module
  • Sending request to get the IP Location Information
  • Extracting the location in JSON format
  • Printing the location extracted
  • Passing the city name and retrieving the weather data of the city
  • Printing the output

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/{}'.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.



Next Article
How to Use Google Cloud Function with Python ?
author
adityaprasad1308
Improve
Article Tags :
  • Python
  • Technical Scripter
  • Python BeautifulSoup
  • Python-requests
  • Technical Scripter 2020
  • Web-scraping
Practice Tags :
  • python

Similar Reads

  • How to extract youtube data in Python?
    Prerequisites: Beautifulsoup YouTube statistics of a YouTube channel can be used for analysis and it can also be extracted using python code. A lot of data like viewCount, subscriberCount, and videoCount can be retrieved. This article discusses 2 ways in which this can be done. Method 1: Using YouTu
    3 min read
  • How to Get Data from API in Python Flask
    In modern web development, APIs (Application Programming Interfaces) play a crucial role in enabling the interaction between different software systems. Flask, a lightweight WSGI web application framework in Python, provides a simple and flexible way to create APIs. In this article, we'll explore ho
    2 min read
  • How to Use Google Cloud Function with Python ?
    Google Cloud Functions provides a way to run small pieces of code in response to cloud events without managing servers. If you're a developer looking to automate tasks, process data or build APIs, Python is a great language for working with Google Cloud Functions. In this article, we will look into
    6 min read
  • How to Automate Google Sheets with Python?
    In this article, we will discuss how to Automate Google Sheets with Python. Pygsheets is a simple python library that can be used to automate Google Sheets through the Google Sheets API. An example use of this library would be to automate the plotting of graphs based on some data in CSV files that w
    4 min read
  • Parsel: How to Extract Text From HTML in Python
    Parsel is a Python library used for extracting data from HTML and XML documents. It provides tools for parsing, navigating, and extracting information using CSS selectors and XPath expressions. Parsel is particularly useful for web scraping tasks where you need to programmatically extract specific d
    2 min read
  • Extract title from a webpage using Python
    Prerequisite Implementing Web Scraping in Python with BeautifulSoup, Python Urllib Module, Tools for Web Scraping In this article, we are going to write python scripts to extract the title form the webpage from the given webpage URL. Method 1: bs4 Beautiful Soup(bs4) is a Python library for pulling
    3 min read
  • Build an Application to extract news from Google News Feed Using Python
    Prerequisite- Python tkinter In this article, we are going to write a python script to extract news articles from Google News Feed by using gnewsclient module and bind it with a GUI application. gnewsclient is a python client for Google News Feed. This API has to installed explicitly first in order
    2 min read
  • How to Extract Script and CSS Files from Web Pages in Python ?
    Prerequisite: RequestsBeautifulSoupFile Handling in Python In this article, we will discuss how to extract Script and CSS Files from Web Pages using Python. For this, we will be downloading the CSS and JavaScript files that were attached to the source code of the website during its coding process. F
    2 min read
  • Extract CSS tag from a given HTML using Python
    Prerequisite: Implementing Web Scraping in Python with BeautifulSoup In this article, we are going to see how to extract CSS from an HTML document or URL using python. Module Needed: bs4: Beautiful Soup(bs4) is a Python library for pulling data out of HTML and XML files. This module does not come bu
    2 min read
  • How to read Emails from Gmail using Gmail API in Python ?
    In this article, we will see how to read Emails from your Gmail using Gmail API in Python. Gmail API is a RESTful API that allows users to interact with your Gmail account and use its features with a Python script. So, let's go ahead and write a simple Python script to read emails. RequirementsPytho
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences