Table of Contents
Introduction
In this tutorial, we will learn how to build a cryptocurrency tracker using Python. By the end of this tutorial, you will be able to fetch the latest cryptocurrency data from an API, parse and display the data using Python, and track the current prices of various cryptocurrencies.
Prerequisites
To follow along with this tutorial, you should have a basic understanding of Python programming language and be familiar with the requests library for making API calls.
Setup
Before we dive into building our cryptocurrency tracker, let’s make sure we have all the necessary software and libraries installed.
-
Python: Make sure you have Python installed on your system. You can download the latest version of Python from the official website: Python Download.
- Requests Library: We will be using the
requests
library to make HTTP requests to the cryptocurrency API. You can install this library using the following command:pip install requests
- Text Editor: You can choose any text editor of your preference, such as Visual Studio Code, Sublime Text, or PyCharm.
Creating a Cryptocurrency Tracker
Step 1: Installing Required Libraries
To begin, let’s install the required libraries. Open your command prompt or terminal and run the following command:
bash
pip install requests
This will install the requests
library, which we will use to make HTTP requests to the cryptocurrency API.
Step 2: Fetching Cryptocurrency Data
Now that we have the necessary libraries installed, let’s start fetching cryptocurrency data from an API. We will be using the CoinGecko API for this purpose. CoinGecko provides a free API that gives access to a wide range of cryptocurrency data.
To fetch the data, we need to make an HTTP GET request to the CoinGecko API. Here’s the code to do that: ```python import requests
def get_cryptocurrency_data():
url = "https://api.coingecko.com/api/v3/coins/markets"
parameters = {
"vs_currency": "usd",
"ids": "bitcoin,ethereum,ripple",
"order": "market_cap_desc",
"per_page": "10",
"page": "1",
"sparkline": "false"
}
response = requests.get(url, params=parameters)
data = response.json()
return data
``` In this example, we are fetching data for three cryptocurrencies: Bitcoin, Ethereum, and Ripple. We are also specifying additional parameters such as the currency, order, number of results per page, and page number.
Step 3: Parsing and Displaying the Data
Now that we have fetched the data, let’s parse and display it. We can extract different properties of each cryptocurrency, such as name, symbol, current price, and market cap. ```python def display_cryptocurrency_data(data): for crypto in data: name = crypto[‘name’] symbol = crypto[‘symbol’] price = crypto[‘current_price’] market_cap = crypto[‘market_cap’]
print(f"{name} ({symbol}): Price - ${price}, Market Cap - ${market_cap}")
``` In this example, we are simply printing the name, symbol, price, and market cap of each cryptocurrency.
Step 4: Putting it All Together
Now that we have defined our functions, let’s put everything together and create a cryptocurrency tracker program that fetches and displays the latest cryptocurrency data. ```python def main(): data = get_cryptocurrency_data() display_cryptocurrency_data(data)
if __name__ == "__main__":
main()
``` This is the main entry point of our program. We call the `get_cryptocurrency_data()` function to fetch the cryptocurrency data and then pass that data to the `display_cryptocurrency_data()` function to display it.
Conclusion
In this tutorial, we learned how to build a cryptocurrency tracker using Python. We covered the steps to fetch cryptocurrency data from an API, parse it, and display it in a readable format. You can further enhance this program by adding more cryptocurrencies, implementing sorting or filtering functionalities, or even building a web interface for tracking the data. With the knowledge gained from this tutorial, you can now explore and analyze various cryptocurrency data using Python.