在 Python 中,有没有办法从 **Google 图片** 搜索结果中下载 all/some 图片文件(例如 JPG/PNG)?

In Python, is there a way I can download all/some the image files (e.g. JPG/PNG) from a **Google Images** search result?

有什么方法可以从 Google 图片 搜索结果中下载 all/some 图片文件(例如 JPG/PNG)?

我可以使用下面的代码下载一个图像,我已经知道它的url:

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())

然后循环遍历 urls 的列表:

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

不过,我真正想做的是从 Google Images[=37 我自己的搜索结果中下载 all/some 图片文件(例如 JPG/PNG) =] 而不必事先有图像列表 urls。

P.S.

拜托,我是一个完全的初学者,我更喜欢一个能够分解实现这一目标的广泛步骤的答案,而不是一个陷入特定代码困境的答案。谢谢。

您可以像这样使用 Google API,其中 BLUE 和 DOG 是您的搜索参数:

https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=BLUE%20DOG

这里有一个开发者指南:

https://developers.google.com/image-search/v1/jsondevguide

您需要解析此 JSON 格式才能直接使用链接。

这是您的 JSON 解析的开始:

import json
j = json.loads('{"one" : "1", "two" : "2", "three" : "3"}')
print(j['two'])