Get Bitcoin Price In Real Time Using Python – randerson112358

The first thing that I like to do before writing logic in my code is to put a description of what the program is about.
#Description: Get the current price of Bitcoin
Next, I will import the request library.
#Import the requests library
import requests
Now let’s store the URL of the ticker, to get the .json file of the cryptocurrencies. This link is the API from coinmarket.com.
TICKER_API_URL = 'https://api.coinmarketcap.com/v1/ticker/'
Let’s create a function to get the latest crypto currency price for a specific ‘crypto’ like Bitcoin, Litecoin or Ethereum.
def get_latest_crypto_price(crypto):response = requests.get(TICKER_API_URL+crypto)
response_json = response.json()
return float(response_json[0]['price_usd'])
Test the function to see if it returns the current price of the crypto currency.
get_latest_crypto_price('bitcoin')
Create a main function to get the current price of the crypto currency, and print any price change when the API updates.
def main():last_price = -1
while True:
crypto = 'bitcoin'
price = get_latest_crypto_price(crypto)
if price != last_price:
print('Bitcoin price: ',price)
last_price = price
Last but not least, we will run the main function.
main()
You can easily get the price of litecoin, ethereum and some other crypto currencies simply by replacing the bitcoin string in this program with the cryptocurrencies of your choice !
Published at Sun, 25 Aug 2019 00:31:33 +0000
Bitcoin Pic Of The Moment
✅ Marco Verch is a Professional Photographer and Speaker from Cologne. 👆 This image can be used under Creative Commons 2.0. Please link to the original photo and the license.
By wuestenigel on 2018-11-28 11:08:51

