Open In App

Scrape Google Search Results using Python BeautifulSoup

In this article, we are going to see how to Scrape Google Search Results using Python BeautifulSoup.

Module Needed:

pip install bs4
pip install requests

Approach:

Example 1: Below is the implementation of the above approach.






# Import the beautifulsoup 
# and request libraries of python.
import requests
import bs4
  
# Make two strings with default google search URL
# our customized search keyword.
# Concatenate them
text= "geeksforgeeks"
  
# Fetch the URL data using requests.get(url),
# store it in a variable, request_result.
request_result=requests.get( url )
  
# Creating soup from the fetched request
soup = bs4.BeautifulSoup(request_result.text,
                         "html.parser")
print(soup)

Output:



Let’s We can do soup.find.all(h3) to grab all major headings of our search result, Iterate through the object and print it as a string.




# soup.find.all( h3 ) to grab 
# all major headings of our search result,
heading_object=soup.find_all( 'h3' )
  
# Iterate through the object 
# and print it as a string.
for info in heading_object:
    print(info.getText())
    print("------")

Output:

Example 2: Below is the implementation. In the form of extracting the city temperature using Google search:




# import module
import requests 
import bs4 
  
# Taking thecity name as an input from the user
city = "Imphal"
  
# Generating the url  
  
# Sending HTTP request 
request_result = requests.get( url )
  
# Pulling HTTP data from internet 
soup = bs4.BeautifulSoup( request_result.text 
                         , "html.parser" )
  
# Finding temperature in Celsius.
# The temperature is stored inside the class "BNeawe". 
temp = soup.find( "div" , class_='BNeawe' ).text 
    
print( temp ) 

Output:


Article Tags :