why couldn't I download images from google with python?
Asked Answered
I

5

18

The code helped me download bunch of images from google. It used to work a few days back and now all of the sudden the code breaks.

Code :

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

Output log:

Item no.: 1 --> Item name = Apple Evaluating... Starting Download...

Unfortunately all 4 could not be downloaded because some images were not downloadable. 0 is all we got for this search filter!

Errors: 0

Item no.: 1 --> Item name = Orange Evaluating... Starting Download...

Unfortunately all 4 could not be downloaded because some images were not downloadable. 0 is all we got for this search filter!

Errors: 0

Item no.: 1 --> Item name = Grapes Evaluating... Starting Download...

Unfortunately all 4 could not be downloaded because some images were not downloadable. 0 is all we got for this search filter!

Errors: 0

Item no.: 1 --> Item name = water melon Evaluating... Starting Download...

Unfortunately all 4 could not be downloaded because some images were not downloadable. 0 is all we got for this search filter!

Errors: 0

This actually create a folder but no images in it.

Installation answered 9/2, 2020 at 8:56 Comment(2)
I don't understand why this post got 2 dislikes ?Installation
I also have same problem. It works fine few days before.Carrousel
O
2

Indeed the issue has appeared not so long ago, there are already a bunch of similar Github issues:

Unfortunately, there is no official solution, for now, you could use the temporary solution that was provided in the discussions.

Orb answered 1/3, 2020 at 17:44 Comment(0)
E
5

google_images_download project is no longer seems compatible wrt Google APIs.

As an alternative you can try simple_image_download.

Encroachment answered 20/8, 2020 at 17:55 Comment(0)
O
2

Indeed the issue has appeared not so long ago, there are already a bunch of similar Github issues:

Unfortunately, there is no official solution, for now, you could use the temporary solution that was provided in the discussions.

Orb answered 1/3, 2020 at 17:44 Comment(0)
R
1

It looks like there is an issue with the package. See these open PRs: PR1 and PR2

Rabaul answered 10/2, 2020 at 14:8 Comment(1)
Visited them long back but still couldn't solve the problemInstallation
J
1

I think Google is changing the DOM. The element class="rg_meta notranslate" is no longer exist. It is changed to 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()
Joggle answered 20/2, 2020 at 9:16 Comment(1)
So, How do I change them?Installation
C
0

The reason this doesn't work is because google changed the way they do everything so that you now need the api_key included in the search string. As a result of this packages such as google-images-download no longer work even if you use the 2.8.0 version because they have no placeholder to insert the api_key string which you must register with Google to get your 2500 free downloads per day.

If you are willing to pay $50 per month or more to access a service from serpapi.com, one way to do this is to use the pip package google-search-results and provide your api_key as part of the query params.

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

where you provide your API key yourself and then call:

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

This returns a JSON string with the link to all the image urls and then you just download them directly.

Cheryl answered 14/4, 2020 at 13:54 Comment(2)
Where do I get the API key?Installation
console.cloud.google.com. You have to provide your login details for google and your credit card, but you can download 25000 items per day without being charged. I'm using it but I've also heard just today that github.com/joeclinton1 has his own variant of the google_images_download code that allows you to download 100 images per day.Cheryl

© 2022 - 2024 — McMap. All rights reserved.