如何使用jquery从api中获取信息?

时间:2018-03-29 21:41:32

标签: javascript jquery html ajax

我想从此网址获取以太坊价格:

https://api.coinmarketcap.com/v1/ticker/ethereum /

我如何获取该信息,并在其发生变化时自动更新?

1 个答案:

答案 0 :(得分:0)

import aiohttp

@client.command()
async def eth(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            value = await resp.json()['price_usd']
            await client.say("Ethereum price is: ${}".format(value))

对于此请求,您似乎有一个端点。硬编码该端点可能更有意义,并且根本没有用户输入任何内容(完全删除url参数并在协程体中有url = 'https://...'

编辑:如上所述,所以

import aiohttp

@client.command()
async def eth():
    url = 'https://api.coinmarketcap.com/v1/ticker/ethereum/'
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            value = await resp.json()['price_usd']
            await client.say("Ethereum price is: ${}".format(value))