How to get the link of the first Image in Google search with Python

I needed a code to find the first image in google search. So I made this code that works to me. In case it does not return any image, there is a way to fix it after this code.

import requests
from bs4 import BeautifulSoup

query = "obama"  # the search query you want to make
url = f"https://www.google.com/search?q={query}&tbm=isch"  # the URL of the search result page

response = requests.get(url)  # make a GET request to the URL
soup = BeautifulSoup(response.text, "html.parser")  # parse the HTML content with BeautifulSoup

# find the first image link by searching for the appropriate tag and attribute
img_tag = soup.find("img", {"class": "yWs4tf"})

if img_tag is not None:
    img_link = img_tag.get("src")
    print(img_link)  # print the first image link
else:
    print("No image found on the page.")

# print(soup.prettify())

In this the case the result is

https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRz2wh2z060PDGYbzOueRY7b4vh2NNH86gUgY7O9AsSt9CRuaukUcfMtUrDtw&s

And if you click on it you will see Obama, one of the greatest president of the USA.

If it returns no image you can add print(soup.prettify())

to the code.

You will get the html code of the google research page. Inspect the page (copy the html code, save it as html, see it in the browser, inspect the page, click the arrow icon, go on the image, look at the class of the image and substitute in the code the class) where you see this statement:

img_tag = soup.find("img", {"class": "yWs4tf"})

This because the tag can change.

Have fun

In this script you can also download the image

import requests
from bs4 import BeautifulSoup
from io import BytesIO
from PIL import Image
import os


def imglink(word):
    ''' lnk to first img in google src '''
    query = word  # the search query you want to make
    url = f"https://www.google.com/search?q={query}&tbm=isch"  # the URL of the search result page

    response = requests.get(url)  # make a GET request to the URL
    soup = BeautifulSoup(response.text, "html.parser")  # parse the HTML content with BeautifulSoup

    # find the first image link by searching for the appropriate tag and attribute
    img_tag = soup.find("img", {"class": "yWs4tf"})

    if img_tag is not None:
        img_link = img_tag.get("src")
        print(img_link)  # print the first image link
        return img_link
    else:
        print("No image found on the page.")

def dl(url):
    ''' download the image '''
    response = requests.get(url)
    # Load the image content into BytesIO object
    image_bytes = BytesIO(response.content)
    # Open the image using PIL
    img = Image.open(image_bytes)
    img.save("immagine.png")
    os.startfile("immagine.png")

cane = imglink("cane")
dl(cane)

Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.