使用xlwings,如何将JSON数据从多个url打印到多个列中?

2024-04-26 19:06:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用grequest从多个url中提取json数据。现在,我想用xlwings将这些结果打印到excel。这是我现在的密码。在

import xlwings as xw
import grequests
import json
urls = [
    'https://bittrex.com/api/v1.1/public/getorderbook?market=BTC-1ST&type=both&depth=50',
    'https://bittrex.com/api/v1.1/public/getorderbook?market=BTC-AMP&type=both&depth=50',
    'https://bittrex.com/api/v1.1/public/getorderbook?market=BTC-ARDR&type=both&depth=50',
]

requests = (grequests.get(u) for u in urls)
responses = grequests.map(requests)

for response in responses:
        BQuantity = [response.json()['result']['buy'][0]['Quantity'],
        response.json()['result']['buy'][1]['Quantity'],
        response.json()['result']['buy'][2]['Quantity'],
        response.json()['result']['buy'][3]['Quantity'],
        response.json()['result']['buy'][4]['Quantity']
]
wb = xw.Book('Book2')
sht = wb.sheets['Sheet1']
sht.range('A1:C5').options(transpose=True).value = BQuantity

这很好,但前提是我注释掉了除一个以外的所有url,否则第一个url的结果将被第二个url的结果覆盖,这是一个预期的结果。然而,这不是我想要的。最后,我希望第一个URL的结果转储到列A,第二个URL的结果转储到列B,等等。。。在

我可以用“请求”(而不是grequests)一个接一个地拉入每个单独的链接,但是,这个操作将由几百个url组成,其中简单地逐个提取数据非常耗时。Grequests在大约8秒的时间内将200个url放入JSON文件,而普通的请求大约需要2分钟。在

任何帮助都将不胜感激。在


Tags: httpsimportcomapijsonurlresponsebuy