📜  如何使用Python下载 Google 图片

📅  最后修改于: 2022-05-13 01:55:34.221000             🧑  作者: Mango

如何使用Python下载 Google 图片

Python是一种多用途语言,广泛用于脚本编写。我们可以编写Python脚本来自动化日常事务。假设我们要下载具有多个搜索查询的谷歌图片。我们可以自动化该过程,而不是手动进行。

如何安装所需的模块:

pip install google_images_download

让我们看看如何使用google_images_download模块编写Python脚本以在Python中下载 Google 图像。

下面是Python代码:

# importing google_images_download module
from google_images_download import google_images_download 
  
# creating object
response = google_images_download.googleimagesdownload() 
  
search_queries = 
[
      
'The smartphone also features an in display fingerprint sensor.',
'The pop up selfie camera is placed aligning with the rear cameras.',
'''In terms of storage Vivo V15 Pro could offer
   up to 6GB of RAM and 128GB of onboard storage.''',
'The smartphone could be fuelled by a 3 700mAh battery.',
]
  
  
def downloadimages(query):
    # keywords is the search query
    # format is the image file format
    # limit is the number of images to be downloaded
    # print urs is to print the image file url
    # size is the image size which can
    # be specified manually ("large, medium, icon")
    # aspect ratio denotes the height width ratio
    # of images to download. ("tall, square, wide, panoramic")
    arguments = {"keywords": query,
                 "format": "jpg",
                 "limit":4,
                 "print_urls":True,
                 "size": "medium",
                 "aspect_ratio":"panoramic"}
    try:
        response.download(arguments)
      
    # Handling File NotFound Error    
    except FileNotFoundError: 
        arguments = {"keywords": query,
                     "format": "jpg",
                     "limit":4,
                     "print_urls":True, 
                     "size": "medium"}
                       
        # Providing arguments for the searched query
        try:
            # Downloading the photos based
            # on the given arguments
            response.download(arguments) 
        except:
            pass
  
# Driver Code
for query in search_queries:
    downloadimages(query) 
    print() 

输出:

注意:由于下载错误,部分图片无法打开。将使用所有图像创建一个单独的“下载”文件夹。