Cryptocurrencies are volatile. No surprise there. But navigating through the sea of price swings can be tricky if you don’t have the right tools. The ability to track prices and capitalize on those quick profit opportunities requires both precision and timing. That’s where a cryptocurrency price tracker comes in handy.
Let’s walk through how to build a straightforward crypto price tracker using Python. In this guide, you’ll scrape live prices for the top 150 cryptocurrencies, including the coin name, ticker, price, and 24-hour percentage change. Ready to get started?
Step 1: Library Imports
First, you’ll need a few libraries to handle the requests and parse the data from HTML:
import requests
from bs4 import BeautifulSoup
import csv
import time
import random
These will allow us to fetch data, parse it, and export the results to a CSV. Easy, right?
Step 2: Proxy Configuration
When you send requests without a proxy, you may get blocked. Modern websites use anti-scraping measures to detect and prevent bots. Here's how to handle that:
proxy = {
"http": "http://Your_proxy_IP_Address:Your_proxy_port",
}
html = requests.get(url, proxies=proxy)
For authenticated proxies, just add your username and password:
proxy = {
"http": "http://username:password@Your_proxy_IP_Address:Your_proxy_port",
}
html = requests.get(url, proxies=proxy)
Now you're set to bypass any "Access Denied" responses.
Step 3: Setting Up Proxy Rotation
To make your scraping more efficient (and undetectable), rotate between different proxies. Scraping with the same proxy too often can get you flagged.
First, create a list of proxies:
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",
"username:password@Your_proxy_IP_Address:Your_proxy_port5",
]
Then, create a method that picks a random proxy from the list:
def get_proxy():
proxy = random.choice(proxies)
return {"http": f'http://{proxy}', "https": f'http://{proxy}'}
This technique ensures your requests appear as if they come from different sources.
Step 4: Scraping Crypto Data
Now, let’s get the actual data. In this case, we’ll be scraping data from the Crypto website. We send a request using our rotating proxy and parse the HTML with BeautifulSoup.
def get_crypto_prices():
url = "https://crypto.com/price"
html = requests.get(url, proxies=get_proxy())
soup = BeautifulSoup(html.text, "html.parser")
We’ll locate the price rows on the webpage, and within each row, extract the coin’s name, ticker, price, and the percentage change in the last 24 hours.
price_rows = soup.find_all('tr', class_='css-1cxc880')
prices = []
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 entry"
coin_ticker_tag = row.find('span', class_='css-1jj7b1a')
ticker = coin_ticker_tag.get_text() if coin_ticker_tag else "no ticker entry"
coin_price_tag = row.find('div', class_='css-b1ilzc')
price = coin_price_tag.text.strip() if coin_price_tag else "no price entry"
coin_percentage_tag = row.find('p', class_='css-yyku61')
percentage = coin_percentage_tag.text.strip() if coin_percentage_tag else "no percentage entry"
prices.append({
"Coin": name,
"Ticker": ticker,
"Price": price,
"24hr-Percentage": percentage
})
return prices
You now have a list of all the important data.
Step 5: Writing Data to CSV
To make the data usable, let’s export it into a CSV file. This will make it easy to analyze later.
def export_to_csv(prices, filename="proxy_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)
Step 6: Launching the Tracker
Finally, we put it all together. We call the get_crypto_prices()
function to get the latest data and export it 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) # Update prices every 5 minutes
Full Script
Here’s the complete code for your crypto price tracker:
import requests
from bs4 import BeautifulSoup
import csv
import time
import random
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",
"username:password@Your_proxy_IP_Address:Your_proxy_port5",
]
def get_proxy():
proxy = random.choice(proxies)
return {"http": f'http://{proxy}', "https": f'http://{proxy}'}
def get_crypto_prices():
url = "https://crypto.com/price"
html = requests.get(url, proxies=get_proxy())
soup = BeautifulSoup(html.content, "html.parser")
price_rows = soup.find_all('tr', class_='css-1cxc880')
prices = []
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 entry"
coin_ticker_tag = row.find('span', class_='css-1jj7b1a')
ticker = coin_ticker_tag.get_text() if coin_ticker_tag else "no ticker entry"
coin_price_tag = row.find('div', class_='css-b1ilzc')
price = coin_price_tag.text.strip() if coin_price_tag else "no price entry"
coin_percentage_tag = row.find('p', class_='css-yyku61')
percentage = coin_percentage_tag.text.strip() if coin_percentage_tag else "no percentage entry"
prices.append({
"Coin": name,
"Ticker": ticker,
"Price": price,
"24hr-Percentage": percentage
})
return prices
def export_to_csv(prices, filename="proxy_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)
if __name__ == "__main__":
while True:
prices = get_crypto_prices()
export_to_csv(prices)
print("Prices updated. Waiting for the next update...")
time.sleep(300)
Final Thoughts
A simple, functional crypto price tracker built with Python allows you to rotate proxies, scrape data at set intervals, and export the results, ensuring access to the latest cryptocurrency prices.
Python’s readability and flexibility make it an ideal choice for creating a scalable and expandable tracker. Adding new features only requires a few lines of code.
Top comments (0)