A Python script that automates cryptocurrency price data extraction from CoinMarketCap API, stores it in a CSV file, and continuously updates the data at regular intervals.
This project fetches real-time cryptocurrency data using the CoinMarketCap API. It retrieves the latest listings, normalizes the data into a structured format using pandas, and saves it into a CSV file for further analysis. The script runs in a loop, appending new data at fixed time intervals.
- Uses CoinMarketCap API to fetch real-time cryptocurrency prices.
- Extracts and normalizes data into a pandas DataFrame.
- Saves data to a CSV file and appends new data at regular intervals.
- Handles API connection errors gracefully.
- Automates data collection for further analysis.
- Python
- Requests (for API calls)
- Pandas (for data manipulation)
- JSON (for parsing API response)
- OS (for file handling)
- Time (for scheduling requests)
Ensure you have the following installed:
- Python 3.x
- pip (Python package manager)
-
Clone this repository:
git clone https://github.com/yourusername/Automating-Crypto-API.git cd Automating-Crypto-API -
Install dependencies:
pip install requests pandas
-
Obtain a CoinMarketCap API key from CoinMarketCap Developer Portal.
-
Replace
'X-CMC_PRO_API_KEY'with your actual API key in the script.
Run the script to start collecting data:
python crypto_api_runner.pyThe script will continuously fetch and store cryptocurrency data every 60 seconds.
from requests import Request, Session
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
import json
import pandas as pd
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': 'your_api_key_here',
}
session = Session()
session.headers.update(headers)
try:
response = session.get(url, params=parameters)
data = json.loads(response.text)
except (ConnectionError, Timeout, TooManyRedirects) as e:
print(e)
df = pd.json_normalize(data['data'])
df['timestamp'] = pd.to_datetime('now')- Establishes a session with the API.
- Retrieves and processes cryptocurrency data.
- Converts JSON response into a pandas DataFrame.
import os
from time import sleep
def api_runner():
global df
try:
response = session.get(url, params=parameters)
data = json.loads(response.text)
df = pd.json_normalize(data['data'])
df['timestamp'] = pd.to_datetime('now')
# Save to CSV
file_path = r'C:\Users\HP\Desktop\API Script\API.csv'
if not os.path.isfile(file_path):
df.to_csv(file_path, header=True)
else:
df.to_csv(file_path, mode='a', header=False)
except Exception as e:
print(e)
for i in range(333):
api_runner()
print('API Runner completed')
sleep(60) # Fetch data every 1 minute- Calls the API every 60 seconds to fetch new data.
- Stores data in a CSV file, appending new records.
- Runs for 333 iterations (~5.5 hours of continuous data collection).