Loading...

Plus+ Python

Working Example for Accessing Bitcoin or Ethereum Public On-Chain Blockchain Data in Python

Working Example for Accessing Bitcoin or Ethereum Public On-Chain Blockchain Data in Python


Public Blockchain Data
One of the big appeals of the blockchain is that they are public and open source, which means anyone can access them and the information they contain. While this is true, it is not always so straight forward to connect and download historical information. Thankfully, there are public resources that scrape data from the blockchain on a daily basis (not live). This working example in Python will allow you to access "on-chain" data for both Bitcoin and Ethereum all the way back to the start of the chain (2009 for Bitcoin!! and 2015 for Ethereum). This on-chain data resource will allow you to access the following fields from the blocks: hash, version, mediantime, nonce, bits, difficulty, chainwork, previousblockhash, size, weight, coinbase_param, number, transaction_count, merkle_root, stripped_size, timestamp, date, last_modified.

There are additional fields captured for the Ethereum chain: difficulty, hash, miner, nonce, number, size, timestamp, total_difficulty, base_fee_per_gas, gas_limit, gas_used, extra_data, logs_bloom, parent_hash, state_root, receipts_root, transactions_root, sha3_uncles

The Code
The code will access the public blockchain Amazon S3 Web bucket resource for a list of dates. You can pass either 'btc' or 'eth' to access either blockchain records. You must also pass a date in the form of a string (ie. 2015-01-01) as a starting point. Afterwards, the data is retrieved in a Pandas DataFrame and then saved as a CSV file for the corresponding date. As always, every line of code is commented. Feel free to edit or fit to your purpose!


This resource is also available via our API or our Plus+ Files


Plus+ membership required

Create a Plus+ account to view the live codebase.

This article has a fully working, notebook-ready code block. Sign up for Plus+ to access it and the rest of the 20+ premium articles.

Subscribe to Plus+ Browse Plus+ articles