File size: 1,400 Bytes
4f2a041
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import requests
import pandas as pd

# Your API Key (you need to replace 'YOUR_API_KEY' with your actual CryptoCompare API key)
api_key = '1606c591d78cde143733502669d97c3707d94637e2c32c3184bef54a2a1653d2'

# The base URL for the CryptoCompare API
base_url = 'https://min-api.cryptocompare.com/data/v2/histohour'

# Parameters for the API call
# In this example, we are getting hourly data for Bitcoin (BTC) priced in USD
parameters = {
    'fsym': 'BTC',
    'tsym': 'USD',
    'limit': 2000,  # The maximum number of data points you can get in one call is 2000
    'api_key': api_key
}

# Make the API call
response = requests.get(base_url, params=parameters)

# Check if the request was successful
if response.status_code == 200:
    # Parse the received JSON data
    data = response.json()

    # Extract the data points
    data_points = data['Data']['Data']

    # Create a DataFrame from the data points
    df = pd.DataFrame(data_points)

    # Convert timestamps to human-readable dates
    df['time'] = pd.to_datetime(df['time'], unit='s')

    # Save the DataFrame to a CSV file
    df.to_csv('crypto_hourly_data.csv', index=False)
    print(df)
    #print("Data extraction successful, saved to crypto_hourly_data.csv")
else:
    print(f"Failed to fetch data: {response.status_code}")

# Make sure to install pandas and requests modules if you haven't already
# pip install pandas requests