[英]Download and SAVE MANY Images from google Image search to a LOCAL FOLDER (Python)
以下代碼可以每次從網站下載一張圖像。 但是,我真正想要實現的是根據查詢從網站上下載許多圖像,然后將這些圖像保存到計算機上的本地文件夾中。
我是編程和python的完整入門者。 我該如何實現?
import urllib.request
file = "Facts.jpg" # file to be written to
url = "http://www.compassion.com/Images/Hunger-Facts.jpg"
response = urllib.request.urlopen (url)
fh = open(file, "wb") #open the file for writing
fh.write(response.read()) # read from request while writing to file
您可以定義一個函數,並使用該函數對要寫入磁盤的每個圖像url重復執行該任務:
def image_request(url, file):
response = urllib.request.urlopen(url)
fh = open(file, "wb") #open the file for writing
fh.write(response.read()) #
例如,如果您有一個包含網址的列表,則可以遍歷該列表:
for i, url in enumerate(urllist):
image_request(url, str(i) + ".jpg")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.