0

I am trying to download images from openi (https://openi.nlm.nih.gov/index.php) using python 3.5 and requests. I am using their rest API as follows:

resp = requests.get("http://openi.nlm.nih.gov/retrieve.php", 
       params = {"query":"Feulgen", "m": 1, "n": 12})
print(resp.content)

This gives me a json file that contains textual information about the images that I would like (including their .png filenames) but I would really like to download the images themselves.

Do I have to download each image according to the specific url listed in the JSON or is there a way to do some sort of "batch download" for the images that would come up in this query?

I have seen this post about downloading images with requests but it seems that I need the .png or .jpeg or .whatever URL. Does anyone know way to get the images without the individual image URLs?

Thanks in advance.

MissBleu
  • 175
  • 2
  • 15

1 Answers1

1

Yes just through and save !

import requests, pprint
resp = requests.get("http://openi.nlm.nih.gov/retrieve.php",
       params = {"query":"Feulgen", "m": 1, "n": 12})
for i in resp.json()['list']:
    #pprint.pprint(i)
    pprint.pprint(i['imgLarge'])
    img = requests.get('http://openi.nlm.nih.gov/'+i['imgLarge'])
    if img.status_code == 200:
        with open("/tmp/"+img.url.split("/")[-1], 'wb') as f:
            f.write(img.content)                                    
mtt2p
  • 1,818
  • 1
  • 15
  • 22