Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
from colorama import init, Fore, Back, Style
import sys
import os
#get the price
def get_price():
#response from the url
response = requests.get(url)
#soup object of the html content
soup = BeautifulSoup(response.content,'html.parser')
#for bitcoin
if asset == 'btc':
price = soup.find('span',{'class':'price'}).text #bitcoin works faster with the price class
#for other altcoins
price = soup.find('span',{'class':'woobJfK-Xb2EM1W1o8yoE'}).text #other altcoins only work with this class
return float(price.replace(",",""))
#asset choice
asset = input('Abbreviation of the asset: ')
url = '' + asset
#catching the NoneType AttributeError error for coins that cant be found
price = get_price()
except AttributeError:
print("The asset doesn't exist or it's not supported!")
if sys.platform == 'win32':
#since the last price must be something from the start its set to 0
price = 0
while True:
#getting the price
last_price = price
price = get_price()
#coloring the price according to the change
if price > last_price:
color = Fore.GREEN
elif last_price > price:
color = Fore.RED
color = Style.RESET_ALL
#printing the price
print('$ ',end='')
print(color + str(price) + Style.RESET_ALL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment