klaptor
klaptor

Reputation: 115

fetch data from API n times and then convert it into a single pandas dataframe

I have this small python script in which i'm fetching cryptocurrency exchange rate data every second.Now I want to stop after fetching data lets say 100 times and then convert all those data into a single dataframe. Also is scheduler the right way to do this?If no then what else should I be using?

import requests
import json
import pandas as pd
import sched, time

s = sched.scheduler(time.time, time.sleep)
def my_function(sc): 
    data = requests.get("https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=BTC,USD,EUR").json()
    print(data)
    s.enter(1, 1, my_function, (sc,))
s.enter(1, 1, my_function, (s,))
s.run()

Upvotes: 3

Views: 160

Answers (1)

andreihondrari
andreihondrari

Reputation: 5833

Maybe something like this:

import requests
import json
import pandas as pd
import time
from matplotlib import pyplot as plt
from pandas import DataFrame

eur_collection = []
usd_collection = []
btc_collection = []

for i in range(100):
    print("Request {}".format(i))
    data = requests.get("https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=BTC,USD,EUR").json()
    eur_collection.append(data["EUR"])
    usd_collection.append(data["USD"])
    btc_collection.append(data["BTC"])
    time.sleep(0.01)

dframe = DataFrame({
    'eur': eur_collection, 
    'usd': usd_collection, 
    'btc': btc_collection
})

dframe.plot()

plt.show()

Upvotes: 2

Related Questions