简体   繁体   English

从Google图片搜索下载许多图像并将其保存到本地文件夹(Python)

[英]Download and SAVE MANY Images from google Image search to a LOCAL FOLDER (Python)

The following code can download from a website one image per time. 以下代码可以每次从网站下载一张图像。 However, what I really want to achieve is to download MANY Images from a website based on a query and then save these images into a LOCAL FOLDER on my computer. 但是,我真正想要实现的是根据查询从网站下载许多图像,然后将这些图像保存到计算机上的本地文件夹中。

I am a complete beginner to programming and python. 我是编程和python的完整入门者。 How can I achieve this? 我该如何实现?

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

You could define a function and use that function to repeat the task for each image url that you would like to write to disk: 您可以定义一个函数,并使用该函数对要写入磁盘的每个图像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 example, if you had a list with urls you could loop over the list: 例如,如果您有一个包含网址的列表,则可以遍历该列表:

for i, url in enumerate(urllist):
    image_request(url, str(i) + ".jpg")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM