【问题标题】:why couldn't I download images from google with python?为什么我不能用 python 从谷歌下载图片?
【发布时间】:2020-02-09 08:56:31
【问题描述】:

代码帮助我从谷歌下载了一堆图片。几天前它曾经工作过,现在突然代码中断了。

代码:

# 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 --> 项目名称 = Apple Evaluating...开始下载...

很遗憾,所有 4 个都无法下载,因为有些图像 不可下载。 0 是我们为这个搜索过滤器得到的全部!

错误:0

Item no.: 1 --> Item name = Orange Evaluating...开始下载...

很遗憾,所有 4 个都无法下载,因为有些图像 不可下载。 0 是我们为这个搜索过滤器得到的全部!

错误:0

Item no.: 1 --> Item name = Grapes Evaluating...开始下载...

很遗憾,所有 4 个都无法下载,因为有些图像 不可下载。 0 是我们为这个搜索过滤器得到的全部!

错误:0

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

很遗憾,所有 4 个都无法下载,因为有些图像 不可下载。 0 是我们为这个搜索过滤器得到的全部!

错误:0

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

【问题讨论】:

  • 我不明白为什么这篇文章有 2 个不喜欢?
  • 我也有同样的问题。几天前它工作正常。

标签: python python-3.x google-image-search


【解决方案1】:

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

您也可以尝试simple_image_download

【讨论】:

    【解决方案2】:

    这个问题确实出现不久,Github上已经有一堆类似的问题了:

    很遗憾,没有官方的解决方案,目前,您可以使用讨论中提供的临时解决方案。

    【讨论】:

      【解决方案3】:

      包裹似乎有问题。查看这些公开 PR:PR1PR2

      【讨论】:

      • 很久以前访问过他们,但仍然无法解决问题
      【解决方案4】:

      我认为 Google 正在改变 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()
      

      【讨论】:

      • 那么,我该如何更改它们?
      【解决方案5】:

      这不起作用的原因是因为谷歌改变了他们做所有事情的方式,所以你现在需要在搜索字符串中包含 api_key。因此,即使您使用 2.8.0 版本,诸如 google-images-download 之类的软件包也不再工作,因为它们没有占位符来插入 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 密钥?
      • console.cloud.google.com。您必须提供您的 google 登录详细信息和您的信用卡,但您每天可以下载 25000 个项目而无需付费。我正在使用它,但我今天还听说github.com/joeclinton1 有他自己的 google_images_download 代码变体,可让您每天下载 100 张图像。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-27
      • 1970-01-01
      • 1970-01-01
      • 2021-02-06
      • 1970-01-01
      • 2017-01-22
      • 1970-01-01
      相关资源
      最近更新 更多