从 REST API 组合多个 ndjson url 的最佳方法是什么?
What is the best way to combine multiple ndjson urls from a REST API?
我的目标是提取所有 url 并向每个 ndjson 文件添加一个获取请求;但是,当有 10 个以上的 url 时,这可能会很复杂。有没有更好的方法来做到这一点,还是我需要放入多个 GET 请求,然后加入 ndjson 文件,然后解析数据。
print(response.text)
输出:
{"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}
list(response.text.values())
输出:
[
"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 请求:
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)
如果我正确理解了你的问题,你发送了一些请求,你 returns 提供了 JSON 对象。您需要从此对象向每个 url 发送请求,并将数据合并到一个容器中(例如 dict
)。
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 个对象的列表,因此如果没有实际数据,就无法帮助编写将这些数据正确存储的代码(供将来解析)格式。
你可以帮助我的国家,检查my profile info。
我的目标是提取所有 url 并向每个 ndjson 文件添加一个获取请求;但是,当有 10 个以上的 url 时,这可能会很复杂。有没有更好的方法来做到这一点,还是我需要放入多个 GET 请求,然后加入 ndjson 文件,然后解析数据。
print(response.text)
输出:
{"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}
list(response.text.values())
输出:
[
"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 请求:
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)
如果我正确理解了你的问题,你发送了一些请求,你 returns 提供了 JSON 对象。您需要从此对象向每个 url 发送请求,并将数据合并到一个容器中(例如 dict
)。
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 个对象的列表,因此如果没有实际数据,就无法帮助编写将这些数据正确存储的代码(供将来解析)格式。
你可以帮助我的国家,检查my profile info。