Why would you ever manually check cryptocurrency prices again if you could write a program to automatically fetch all prices from Coinmarketcap and output them in your Terminal?
I would´t either so let´s start. We need some really basic prerequisites.
- Python 3.6
- editor of choice
Setting up your Environment
I assume you already have a basic understanding how Python works but actually you should still follow along pretty easily since the syntax is not that hard.
Before we can start hacking our code we need to check coinmarketcap html structure to see where and how the information is titled and stored.
Luckily for us, coinmarketcap contains a API here which automatically provided us with a nicely formatted Json file.
Writing the code
Now we can start hacking...
Fire up your favorite Texteditor or IDE and start importing the first and only module request by typing:
specify your url like:
url = https://api.coinmarketcap.com/v1/ticker/?convert=EUR&Limit=10
using the request library we can make use of the
function which will provide us with a list.
We would like to say that for every coin in the list we are going to print the name of the coin and the price in usd.
Let´s wrap this in some code!
for coin in requests.get(url).json(): print(coin['name], coin['price_usd])
that was basically it! Pretty easy right?
Actually this program was never intended as a standalone version rather as a library to use in a Telegram Chatbot or to automatic monitor the cryptocurrency prices.
You can find the gist here.
There is also a more complex method in getting the prices directly from the html code which is slower but can be used in a more general way using BeautifulSoup. You can get the gist here