Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report