Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does Python query the real-time price of Bitcoin?

Shulou Source: shulou.com Published: 2022-06-01 08:46:24 09月27日 Update

This article mainly introduces "how to query the real-time price of bitcoin by Python". In the daily operation, I believe that many people have doubts about how to query the real-time price of bitcoin by Python. The editor has consulted all kinds of information and sorted out a simple and easy-to-use operation method. I hope it will be helpful to answer the doubt about "how to query the real-time price of bitcoin by Python". Next, please follow the editor to study!

1. Bitcoin quotation API call code

I like to write a comment to illustrate the purpose of the program before writing the implementation logic:

# description: get the real-time price of Bitcoin

Next I will import the requests library:

# Import requests library import requests

Now let's save the URL of Bitcoin API. We can use the API provided by coinmarketcap.com:

TICKER_API_URL = 'https://api.coinmarketcap.com/v1/ticker/'

Now let's create a function to get the price of a specified digital currency such as Bitcoin, Lettercoin, or Ethernet Square.

Def get_latest_crypto_price (crypto): response = requests.get (TICKER_API_URL+crypto) response_json = response.json () return float (response_json [0] ['price_usd'])

Call this function to test:

Get_latest_crypto_price ('bitcoin')

The test results are as follows:

Now we create a main function to get the current price of the specified digital currency, and then display the output:

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

Just run the main function:

Main ()

The running results are as follows:

In the above code, you can easily query the real-time prices of other digital currencies, such as Wright coins, Ethernet coins, by simply replacing the value of the crypto variable!

At this point, the study on "how to query the real-time price of bitcoin by Python" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Prices real-time queries functions learning numbers money next code more results quotes Wright Ethernet help testing running utility variables I like Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Information Docker MariaDB Linux