Skip to content

Instantly share code, notes, and snippets.

@cesarmiquel
Last active February 22, 2021 16:25
Show Gist options
  • Save cesarmiquel/9a77b42c2ac40cbda3758e4d81a5fafd to your computer and use it in GitHub Desktop.
Save cesarmiquel/9a77b42c2ac40cbda3758e4d81a5fafd to your computer and use it in GitHub Desktop.
Scrape Bitstamp for currency
import requests
import datetime
import os
import time
import json
# -------------------------------------------------------
#
# User Bitstamp API to scrape current data. See:
#
# https://www.bitstamp.net/api/
#
# for more information.
#
# -------------------------------------------------------
# Create dir if its not there
dir = time.strftime("/home/ml/crypto/scraper/data/%Y/%m/%d")
os.makedirs(dir, exist_ok=True)
timestamp = int(time.time())
currency_pairs = ['btcusd', 'ethusd', 'xrpusd', 'ltcusd']
response = {}
for currency_pair in currency_pairs:
url = "https://www.bitstamp.net/api/v2/ticker/{0}".format(currency_pair)
r = requests.get(url)
response[currency_pair] = r.json()
filename = "{0}/{1}.json".format(dir, timestamp)
fp = open(filename, 'w')
fp.write(json.dumps(response))
fp.close()
print("Wrote [{0}]".format(filename))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment