Home > Back-end >  How do I get only the price of ticker from Kucoin using API in Python
How do I get only the price of ticker from Kucoin using API in Python

Time:06-10

all i want is to print out only the price

example: 30171.1

this is my code bellow

from webbrowser import get
import requests
from kucoin.client import Client
url = 'https://api.example.com'

ticker = requests.get(url   '/api/v1/market/orderbook/level1?symbol=BTC-USDT').json()
tk = (ticker.get('code', 'price'))
a = float(tk)
print(a)
print('===================================================================================================================================')
#print(ticker)

rex = requests.get(url   '/api/v1/market/orderbook/level1?symbol=BTC-USDT', data = { 'data': 'price'} ).json()
print(rex)

this is the output

200000.0
===================================================================================================================================
{'code': '200000', 'data': {'time': 1654811646376, 'sequence': '1630785467667', 'price': '30171.1', 'size': '0.00083', 'bestBid': '30171.1', 'bestBidSize': '0.20173654', 'bestAsk': '30171.2', 'bestAskSize': '0.39403466'}}

CodePudding user response:

Here you go:

from webbrowser import get
import requests
url = 'https://api.kucoin.com'

ticker = requests.get(url   '/api/v1/market/orderbook/level1?symbol=BTC-USDT').json()
tk = (ticker.get('code', 'price'))
a = float(tk)
#print(a)
#print('===================================================================================================================================')

rex = requests.get(url   '/api/v1/market/orderbook/level1?symbol=BTC-USDT', data = { 'data': 'price'} ).json()
print(rex['data']['price'])

You might want to learn about Python Dictionaries here.

  • Related