0

I am using google's books API to fetch information about books based on ISBN number. I am getting thumbnails in response along with other information. The response looks like this:

 "imageLinks": {
 "smallThumbnail": "http://books.google.com/books/content?id=tEDhAAAAMAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api",
 "thumbnail": "http://books.google.com/books/content?id=tEDhAAAAMAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api"
},

I want to download thumbnails on the links above and store them on local file system. How can it be done python?

saim2025
  • 280
  • 2
  • 5
  • 14
  • Taking into account the answers below: don't forget to implement try/except for `requests` exceptions. There's quite a lot to consider. [Requests 2.19.1 - Exceptions](http://docs.python-requests.org/en/latest/api/#exceptions) – Noah M. Aug 01 '18 at 13:16
  • you can get the picture from the url as described here- https://stackoverflow.com/questions/19602931/basic-http-file-downloading-and-saving-to-disk-in-python#19602990 – Amitay Dror Aug 01 '18 at 13:16

1 Answers1

1

Use urllib module

Ex:

import urllib

d = {"imageLinks": {
     "smallThumbnail": "http://books.google.com/books/content?id=tEDhAAAAMAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api",
     "thumbnail": "http://books.google.com/books/content?id=tEDhAAAAMAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api"
     }
}

urllib.urlretrieve(d["imageLinks"]["thumbnail"], "MyThumbNail.jpg")

Python3X

from urllib import request

with open("MyThumbNail.jpg", "wb") as infile:
    infile.write(request.urlopen(d["imageLinks"]["thumbnail"]).read())
Rakesh
  • 81,458
  • 17
  • 76
  • 113