首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从REST组合多个ndjson urls的最佳方法是什么?

从REST组合多个ndjson urls的最佳方法是什么?
EN

Stack Overflow用户
提问于 2022-03-30 20:24:53
回答 1查看 60关注 0票数 0

我的目标是提取所有的urls并向每个ndjson文件添加一个get请求;但是,当有更多的10个urls时,这可能会很复杂。是否有更好的方法来做到这一点,或者我需要将多个GET请求放入其中,然后加入ndjson文件,然后解析数据。

代码语言:javascript
复制
print(response.text)

输出:

代码语言:javascript
复制
{"transactionTime":"2022-03-27T08:51:32.174-04:00","request":"https://api.site/data/5555/$export","requiresAccessToken":true,"output": [
        {
            "type":"robot",
            "url":"https://api.site/data/5555/838916.ndjson"
        },
        {
            "type":"robot",
            "url":"https://api.site/data/5555/838917.ndjson"
        },
        {
            "type":"robot",
            "url":"https://api.site/data/5555/838918.ndjson"
        }
    ]
"error":[],"JobID":12443}
代码语言:javascript
复制
list(response.text.values())

输出:

代码语言:javascript
复制
 [
    "1990-01-28T08:51:32.174-04:00",
    "https://api.site/data/5555/$export",
    true,
    [
        {
            "type":"robot",
            "url":"https://api.site/data/5555/838916.ndjson"
        },
        {
            "type":"robot",
            "url":"https://api.site/data/5555/838917.ndjson"
        },
        {
            "type":"robot",
            "url":"https://api.site/data/5555/838918.ndjson"
        }
    ]

我目前在这里添加了多个GET请求:

代码语言:javascript
复制
response1 = requests.get("https://api.site/data/5555/838916.ndjson",headers=headers)
response2 = requests.get("https://api.site/data/5555/838917.ndjson",headers=headers)
response3 = requests.get("https://api.site/data/5555/838918.ndjson",headers=headers)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-30 21:21:12

如果我正确理解了您的问题,您将发送一些返回您提供的JSON对象的请求。您需要从这个对象向每个url发送请求,并将数据合并到一个容器中(例如,dict)。

代码语言:javascript
复制
from requests import Session

headers = { ... }  # some headers

sess = Session()
sess.headers.update(headers)

resp = sess.get("https://api.site/data/5555/$export")
for item in resp.json()["output"]:
    ndjson = sess.get(item["url"])
    # here some code to process ndjson.text

通常,ndjson是一个由换行符分隔的JSON对象列表,因此如果没有实际数据,就不可能使用代码来帮助以正确的(用于将来解析)格式存储数据。

你可以帮助我的国家,检查。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71683725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档