如何使用Python下载Google图像
Google提供了许多Python包,可以最小化编写Python代码从Google服务获取数据的工作量。其中一个包是Google图像下载。它将关键字作为参数,定位具有这些关键字的图像。
阅读更多:Python 教程
示例
在下面的示例中,我们将图像数量限制为5,并允许程序打印生成文件的URL。
from google_images_download import google_images_download
#实例化类
response = google_images_download.googleimagesdownload()
arguments = {"keywords":"lilly,hills","limit":5,"print_urls":True}
paths = response.download(arguments)
#打印下载图像的完整路径
print(paths)
输出
运行上述代码会给我们以下结果#8211;。
Image URL: https://assets.traveltriangle.com/blog/wp-content/uploads/2017/11/Hill-Stations-Near-Kolkata-cover1-400x267.jpg
Completed Image ====> 4.Hill-Stations-Near-Kolkata-cover1-400x267.jpg
Image URL: https://image.shutterstock.com/image-photo/distant-hills-hilly-steppe-curvy-260nw-1037414248.jpg
Completed Image ====> 5.distant-hills-hilly-steppe-curvy-260nw-1037414248.jpg
({'lilly': ['C:\python3\downloads\lilly\1.Lilly-Tougas.jpg',
'C:\python3\downloads\lilly\2.1200px-Eli_Lilly_and_Company.svg.png', '
C:\python3\downloads\lilly\3.nikki-lilly-this-morning.jpg',
'C:\python3\downloads\lilly\4.lily-plants.jpg',
'C:\python3\downloads\lilly\5.dish-lilly-ghalichi.jpg'],
'hills': ['C:\python3\downloads\hills\1.220px-Clouds_over_hills.jpg',
'C:\python3\downloads\hills\2.Bacin_zari_2015.jpg',
'C:\python3\downloads\hills\3.65ad9ac0-0455-4086-a4f4-1245f697d10e.png',
'C:\python3\downloads\hills\4.Hill-Stations-Near-Kolkata-cover1-400x267.jpg',
'C:\python3\downloads\hills\5.distant-hills-hilly-steppe-curvy-260nw-1037414248.jpg']}, 1)