You can also use the the API of Coingecko if you dont want to use a specific exchange.
For fetching the current btc price in USD you can use this url:
https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usdYou are good to go with the version of bitmover but here is a version where the API is used from coingecko:
import requests
def get_bitcoin_price():
url = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd"
response = requests.get(url)
# request check
if response.status_code == 200:
data = response.json()
bitcoin_price = data["bitcoin"]["usd"]
return bitcoin_price
else:
print(f"Error {response.status_code}: Sorry, but unable to fetch current price of Bitcoin from Coingecko.")
return None
if __name__ == "__main__":
price = get_bitcoin_price()
if price:
print(f"The current price of Bitcoin is ${price:,.2f} USD.")
Implemented check if the request to the API was successful, otherwise they would be following output:
Sorry, but unable to fetch current price of Bitcoin from Coingecko.