DEV Community

What You Should Know About Crypto Price Tracker

Cryptocurrency markets are notorious for their volatility. Prices can swing wildly in a matter of minutes, making it crucial to track real-time data to capitalize on profit opportunities. But how do you get reliable, accurate pricing information? It’s not always easy. Free APIs have limitations, and accessing the right data at the right time can be tricky. That’s where building your own price tracker comes in.

Step 1: Import Required Libraries

To get started, we need the right tools. We’ll use Python’s requests library to fetch data and BeautifulSoup from bs4 to parse the HTML. We’ll also use csv for saving the data and time and random to control the frequency of updates and rotate proxies.
Here’s the list of libraries you need:

import requests
from bs4 import BeautifulSoup
import csv
import time
import random
Enter fullscreen mode Exit fullscreen mode

Step 2: Set Up Proxy Connections

Cryptocurrency sites often block multiple requests from the same IP address. This means if you're scraping data repeatedly from one location, you might get blocked. To avoid this, we'll rotate proxies. Here's how you can set it up:

proxy = {
    "http": "http://Your_proxy_IP_Address:Your_proxy_port",
}
html = requests.get(url, proxies=proxy)
Enter fullscreen mode Exit fullscreen mode

For authenticated proxies, use:

proxy = {
    "http": "http://username:password@Your_proxy_IP_Address:Your_proxy_port",
}
html = requests.get(url, proxies=proxy)
Enter fullscreen mode Exit fullscreen mode

Step 3: Handling Proxy Rotation

To further avoid detection, we'll randomly choose proxies from a list. Here’s how:

proxies = [
    "username:password@Your_proxy_IP_Address:Your_proxy_port1",
    "username:password@Your_proxy_IP_Address:Your_proxy_port2",
    "username:password@Your_proxy_IP_Address:Your_proxy_port3",
    "username:password@Your_proxy_IP_Address:Your_proxy_port4",
]

def get_proxy():
    proxy = random.choice(proxies)
    return {"http": f'http://{proxy}', "https": f'http://{proxy}'}
Enter fullscreen mode Exit fullscreen mode

Each time the script sends a request, it will randomly pick a proxy from the list, making it harder for websites to block you.

Step 4: Scraping Cryptocurrency Data

Next, we need to scrape the price data. We'll target a crypto price site and set it up like this:

def get_crypto_prices():
    url = "https://crypto.com/price"
    html = requests.get(url, proxies=get_proxy())
    soup = BeautifulSoup(html.text, "html.parser")
Enter fullscreen mode Exit fullscreen mode

Step 5: Inspecting the Site Structure

Before we can scrape, we need to understand how the data is structured on the page. Open the site in your browser, right-click, and choose “Inspect” to explore the HTML.
Once you've identified the relevant HTML elements, we can use BeautifulSoup to extract the desired information. For example:

price_rows = soup.find_all('tr', class_='css-1cxc880')
Enter fullscreen mode Exit fullscreen mode

Now, for each row, we’ll extract the coin name, ticker symbol, price, and 24-hour percentage change. Here's the code for extracting this data:

for row in price_rows:
    coin_name_tag = row.find('p', class_='css-rkws3')
    name = coin_name_tag.get_text() if coin_name_tag else "No name"

    coin_ticker_tag = row.find('span', class_='css-1jj7b1a')
    ticker = coin_ticker_tag.get_text() if coin_ticker_tag else "No ticker"

    coin_price_tag = row.find('div', class_='css-b1ilzc')
    price = coin_price_tag.text.strip() if coin_price_tag else "No price"

    coin_percentage_tag = row.find('p', class_='css-yyku61')
    percentage = coin_percentage_tag.text.strip() if coin_percentage_tag else "No percentage"

    prices.append({
        "Coin": name,
        "Ticker": ticker,
        "Price": price,
        "24hr-Percentage": percentage
    })
Enter fullscreen mode Exit fullscreen mode

Step 6: Saving Results in CSV

Once we’ve scraped the data, we’ll save it to a CSV file for easy access. This function writes the data to a CSV file:

def export_to_csv(prices, filename="crypto_prices.csv"):
    with open(filename, "w", newline="") as file:
        fieldnames = ["Coin", "Ticker", "Price", "24hr-Percentage"]
        writer = csv.DictWriter(file, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerows(prices)
Enter fullscreen mode Exit fullscreen mode

Step 7: Activating the Tracker

To keep the tracker running and updating the prices, we’ll set it to scrape and update every 5 minutes:

if __name__ == "__main__":
    while True:
        prices = get_crypto_prices()
        export_to_csv(prices)
        print("Prices updated. Waiting for the next update...")
        time.sleep(300)  # Wait for 5 minutes before next update
Enter fullscreen mode Exit fullscreen mode

Final Thoughts

This simple cryptocurrency price tracker script is just the beginning. Python’s flexibility allows you to expand the project by adding features like automatic notifications, advanced data analysis, or integration with trading platforms. You now have a foundation for building your own crypto price tracker and tracking the dynamic world of crypto prices, with countless possibilities for what can be added next.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay