Get Real-time Crypto Price Using Python And Binance API – GeeksforGeeks
In this article, we are going to see how to get the real-time price of cryptocurrencies using Binance API in Python.
Mục lục bài viết
Binance API
Binance API is a method that allows you to connect to the Binance servers using several programming languages. With it, you can automate your trading and make HTTP requests to send and receive data.
Here we access Binance API using Python with requests module. We will be sending requests to Binance API and extracting the real-time price of the required cryptocurrency in JSON format. We will use JSON module to convert extracted JSON data to a Python dictionary.
Example 1: Get Crypto Price Using Python And Binance API
Here requests.get() will send a request to a specified URL and save it in data and json() converted data to a Python dictionary.
Python3
import
json
import
requests
key
=
"https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT"
data
=
requests.get(key)
data
=
data.json()
print
(f
"{data['symbol']} price is {data['price']}"
)
Output:
BTCUSDT price is 41522.20000000
Example 2: Get Multiple Real-time Crypto prices
Python3
import
json
import
requests
key
=
"https://api.binance.com/api/v3/ticker/price?symbol="
currencies
=
[
"BTCUSDT"
,
"DOGEUSDT"
,
"LTCUSDT"
]
j
=
0
for
i
in
currencies:
url
=
key
+
currencies[j]
data
=
requests.get(url)
data
=
data.json()
j
=
j
+
1
print
(f
"{data['symbol']} price is {data['price']}"
)
Output:
BTCUSDT price is 41522.20000000 DOGEUSDT price is 0.14710000 LTCUSDT price is 125.00000000
My Personal Notes
arrow_drop_up