【问题标题】:recursive image download with requests带有请求的递归图像下载
【发布时间】:2018-03-20 19:21:40
【问题描述】:

我正在尝试使用 python 3.5 和请求从 openi (https://openi.nlm.nih.gov/index.php) 下载图像。我正在使用他们的rest API如下:

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

这给了我一个 json 文件,其中包含有关我想要的图像的文本信息(包括它们的 .png 文件名),但我真的很想自己下载图像。

我是否必须根据 JSON 中列出的特定 url 下载每个图像,或者有没有办法对此查询中出现的图像进行某种“批量下载”?

我已经看到 this post 关于下载带有请求的图像,但似乎我需要 .png 或 .jpeg 或 .whatever URL。有谁知道在没有单个图像 URL 的情况下获取图像的方法?

提前致谢。

【问题讨论】:

  • 是的..您必须为每个预期的 URL 调用图像下载功能

标签: python-3.x python-requests


【解决方案1】:

是的,只需通过并保存!

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)                                    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-29
    • 2020-05-13
    相关资源
    最近更新 更多