我如何使用jquery从api获取信息? [英] How do I fetch information from a api with jquery?

查看:133
本文介绍了我如何使用jquery从api获取信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



如何获取该信息,并在每次更改时自动更新它?

解决方案

  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))


I want to fetch ethereums price from this url:

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

How would I get that information, and have it automatically update it whenever it changes?

解决方案

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))

It seems like you have an endpoint in mind for this request. It probably makes more sense to hardcode that endpoint and don't have the user enter anything at all (remove the url argument entirely and have a url = 'https://...' line in the coroutine body)

Edit: as above, so below

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))

这篇关于我如何使用jquery从api获取信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆