如何使用 Python 下載 Google 圖片


Google 提供了許多 Python 軟體包,這些軟體包最大限度地減少了編寫 Python 程式碼以從 Google 服務獲取資料的工作。下載 Google 圖片就是這樣一個軟體包。它以關鍵字作為引數,並找到具有這些關鍵字的圖片。

示例

在以下示例中,我們將圖片數量限制為 5,並允許程式打印出生成檔案時的網址。

from google_images_download import google_images_download
#instantiate the class
response = google_images_download.googleimagesdownload()
arguments = {"keywords":"lilly,hills","limit":5,"print_urls":True}
paths = response.download(arguments)
#print complete paths to the downloaded images
print(paths)

輸出

執行以上程式碼會得到以下結果:-

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)

更新日期: 08-Aug-2019

1K+ 瀏覽

開啟你的 職業生涯

完成課程認證

開始
廣告