This is how you can get the first image google finds of a subject and you can also download it
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
My youtube channel
Twitter: @pythonprogrammi - python_pygame