为什么我用Python无法从谷歌下载图片?

16

这段代码帮助我从谷歌下载了很多图片。它在几天前还可以使用,但是现在突然出问题了。

代码:

# importing google_images_download module 
from google_images_download import google_images_download  

# creating object 
response = google_images_download.googleimagesdownload()  

search_queries = ['Apple', 'Orange', 'Grapes', 'water melon'] 


def downloadimages(query): 
    # keywords is the search query 
    # format is the image file format 
    # limit is the number of images to be downloaded 
    # print urs is to print the image file url 
    # size is the image size which can 
    # be specified manually ("large, medium, icon") 
    # aspect ratio denotes the height width ratio 
    # of images to download. ("tall, square, wide, panoramic") 
    arguments = {"keywords": query, 
                 "format": "jpg", 
                 "limit":4, 
                 "print_urls":True, 
                 "size": "medium", 
                 "aspect_ratio": "panoramic"} 
    try: 
        response.download(arguments) 

    # Handling File NotFound Error     
    except FileNotFoundError:  
        arguments = {"keywords": query, 
                     "format": "jpg", 
                     "limit":4, 
                     "print_urls":True,  
                     "size": "medium"} 

        # Providing arguments for the searched query 
        try: 
            # Downloading the photos based 
            # on the given arguments 
            response.download(arguments)  
        except: 
            pass

# Driver Code 
for query in search_queries: 
    downloadimages(query)  
    print()

输出日志:

项目编号:1 --> 项目名称 = 苹果 评估中... 开始下载...

抱歉,由于一些图像无法下载,所有4个图像都无法下载。对于此搜索过滤器,我们只得到了0!

错误:0

项目编号:1 --> 项目名称 = 橙子 评估中... 开始下载...

抱歉,由于一些图像无法下载,所有4个图像都无法下载。对于此搜索过滤器,我们只得到了0!

错误:0

项目编号:1 --> 项目名称 = 葡萄 评估中... 开始下载...

抱歉,由于一些图像无法下载,所有4个图像都无法下载。对于此搜索过滤器,我们只得到了0!

错误:0

项目编号:1 --> 项目名称 = 西瓜 评估中... 开始下载...

抱歉,由于一些图像无法下载,所有4个图像都无法下载。对于此搜索过滤器,我们只得到了0!

错误:0

实际上创建了一个文件夹,但其中没有图像。


2
我不明白为什么这篇帖子会有两个踩? - Sai Krishnadas
1
我也有同样的问题。几天前它还能正常工作。 - Amith Dissanayaka
5个回答

4

google_images_download 项目似乎不再与Google APIs兼容。

作为替代方案,您可以尝试使用simple_image_download


2

1

看起来这个包有问题。请查看这些未解决的PR:PR1PR2


1
访问过他们很久了,但仍然无法解决问题。 - Sai Krishnadas

1

我认为谷歌正在更改DOM。元素class="rg_meta notranslate"不再存在。它被更改为class="rg_i ..."


def get_soup(url,header):
    return BeautifulSoup(urllib2.urlopen(urllib2.Request(url,headers=header)),'html.parser')    

def main(args):
    query = "typical face"
    query = query.split()
    query = '+'.join(query)
    url = "https://www.google.co.in/search?q="+query+"&source=lnms&tbm=isch"
    headers = {}
    headers['User-Agent'] = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"
    soup = get_soup(url, headers)
    for a in soup.find_all("img", {"class": "rg_i"}):
        wget.download(a.attrs["data-iurl"], a.attrs["data-iid"])


if __name__ == '__main__':
    from sys import argv
    try:
        main(argv)
    except KeyboardInterrupt:
        pass
    sys.exit()

那么,我该如何更改它们? - Sai Krishnadas

0

这个不起作用的原因是因为谷歌改变了他们做事情的方式,所以现在你需要在搜索字符串中包含 api_key。由于这个原因,像 google-images-download 这样的软件包不再工作,即使你使用 2.8.0 版本,因为它们没有占位符来插入 api_key 字符串,你必须向 Google 注册才能获得每天 2500 次免费下载。

如果你愿意支付每月 $50 或更多的费用来访问 serpapi.com 的服务,一种方法是使用 pip 软件包 google-search-results 并将你的 api_key 提供为查询参数的一部分。

params = {
           "engine" : "google",
           ...
           "api_key" : "secret_api_key" 
}

在这里您需要自己提供 API 密钥,然后调用:

client = GoogleSearchResults(params)
results = client.get_dict()

这将返回一个JSON字符串,其中包含所有图像URL的链接,然后您可以直接下载它们。


我在哪里获取API密钥? - Sai Krishnadas
1
https://console.cloud.google.com。您需要提供Google的登录详细信息和信用卡,但每天可以免费下载25000个项目而不收费。我正在使用它,但今天刚听说https://github.com/joeclinton1有自己的变体google_images_download代码,允许您每天下载100张图片。 - Eamonn Kenny

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接