Skip to content
Automating CoinMarketCap API Pull Using Python
from requests import Request, Session
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
import json
url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
parameters = {
'start':'1',
'limit':'50',
'convert':'USD'
}
headers = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': 'c0948cf3-d185-450f-a107-75e468d020cd',
}
session = Session()
session.headers.update(headers)
try:
response = session.get(url, params=parameters)
data = json.loads(response.text)
print(data)
except (ConnectionError, Timeout, TooManyRedirects) as e:
print(e)type(data)import pandas as pd
#This allows you to see all the columns, not just like 15
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
#pd.set_option('display.max_rows', None)#This normalizes the data and makes it all pretty in a dataframe
df = pd.json_normalize(data['data'])
df['timestamp']=pd.to_datetime('now')
dfdef api_runner():
global df
url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
parameters = {
'start':'1',
'limit':'15',
'convert':'USD'
}
headers = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': 'c0948cf3-d185-450f-a107-75e468d020cd',
}
session = Session()
session.headers.update(headers)
try:
response = session.get(url, params=parameters)
data = json.loads(response.text)
print(data)
except (ConnectionError, Timeout, TooManyRedirects) as e:
print(e)
df2 = pd.json_normalize(data['data'])
df2['timestamp']=pd.to_datetime('now')
df_append = pd.DataFrame(df2)
df = pd.concat([df, df_append])
#if you want to save it in a csv format each time, can use the code below
#df = pd.json_normalize(data['data'])
#df['timestamp']=pd.to_datetime('now')
#df
#if not os.path.isfile(r'C:\Users\sylvi\OneDrive\Documents\Python Scripts\API.csv'):
#df.to_csv(r'C:\Users\sylvi\OneDrive\Documents\Python Scripts\API.csv', header='column_names')
#else:
#df.to_csv(r'C:\Users\sylvi\OneDrive\Documents\Python Scripts\API.csv', mode='a', header=False)import os
from time import time
from time import sleep
for i in range(333):
api_runner()
print('API Runner completed Successfully')
sleep(60) # sleep for 1 minute
exit()df# One thing I noticed was the scientific notation. I like it, but I want to be able to see the numbers in this case
pd.set_option('display.float_format', lambda x: '%.5f' % x)
df# Now let's look at the coin trends over time in average
df3 = df.groupby('name', sort=False)[['quote.USD.percent_change_1h','quote.USD.percent_change_24h','quote.USD.percent_change_7d','quote.USD.percent_change_30d','quote.USD.percent_change_60d','quote.USD.percent_change_90d']].mean()
df3
df4= df3.stack()
df4type(df4)# change the df4 back to data frame
df5 = df4.to_frame(name='Value')
df5type(df5)df5.count()index = pd.Index(range(300))
df6 = df5.reset_index()
df6