在Python中,有没有一种方法可以从** Google图片**搜索结果中下载全部/部分图片文件(例如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图片搜索结果中下载全部/部分图片文件(例如JPG/PNG)?
Is there a way I can download all/some the image files (e.g. JPG/PNG) from a Google Images search result?
我可以使用以下代码下载已经知道其网址的 图片:
I can use the following code to download one image that I already know its 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重复执行此任务.磁盘:
To download multiple images, it has been suggested that I define a function and use that function to repeat the task for each image url that I would like to write to disk:
def image_request(url, file):
response = urllib.request.urlopen(url)
fh = open(file, "wb") #open the file for writing
fh.write(response.read())
然后使用以下方法遍历网址列表:
And then loop over a list of urls with:
for i, url in enumerate(urllist):
image_request(url, str(i) + ".jpg")
但是,我真正想做的是从我自己的搜索结果中从 Google图片下载全部/某些图片文件(例如JPG/PNG),而不必事先获得图片网址的列表.
However, what I really want to do is download all/some image files (e.g. JPG/PNG) from my own search result from Google Images without necessarily having a list of the image urls beforehand.
P.S.
请问我是一个完整的初学者,并且希望能得到一个答案,该答案将打破实现该目标的主要步骤,而不是使特定代码陷入僵局的步骤.谢谢.
Please I am a complete beginner and would favour an answer that breaks down the broad steps to achieve this over one that is bogs down on specific codes. Thanks.
推荐答案
您可以像这样使用Google API,其中BLUE和DOG是您的搜索参数:
You can use the Google API like this, where BLUE and DOG are your search parameters:
https://ajax.googleapis .com/ajax/services/search/images?v = 1.0& q = BLUE%20DOG
这里有关于此的开发者指南:
There is a developer guide about this here:
https://developers.google.com/image-search/v1/jsondevguide
您需要先解析此JSON格式,然后才能直接使用链接.
You need to parse this JSON format before you can use the links directly.
这是JSON解析的开始:
Here's a start to your JSON parsing:
import json
j = json.loads('{"one" : "1", "two" : "2", "three" : "3"}')
print(j['two'])
这篇关于在Python中,有没有一种方法可以从** Google图片**搜索结果中下载全部/部分图片文件(例如JPG/PNG)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!