This article uses Mac OS Sierra 10.12.4. The python version is 3.5.0.
Bitcoin and other virtual currencies have an explorer for checking addresses and transactions. Etherscan is available as an Explorer for Ethereum. And Etherscan also has an API. If you use this, you can easily extract transactions without going directly from the blockchain. So I will use it.
To use the Etherscan API, you need to register Etherscan and obtain the API-KEY. The registration and acquisition procedure is described on the official Etherscan API page. You can get the API-KEY by newly registering with Etherscan and looking at ClientPortal-> MyApiKey.
This time, we will use etherscan api using python. To do this, go to the py-etherscan-api github page (https://github.com/corpetty/py-etherscan-api/tree/cebc169dfca7d4fea5eec01c875550216de67039) and git clone it.
git-clone
git clone https://github.com/corpetty/py-etherscan-api.git
python
cd py-etherscan-api
Install locally after cloning.
python
python setup.py install
Etherscan ALL Account Obtain an appropriate address from the top of the amount and confirm the transaction.
get_all_transactions ()
was an error (Reference: [get_all_transactions leads to error in connect # 34]
](Https://github.com/corpetty/py-etherscan-api/issues/34)), changed to the method get_transaction_page ()
. If you want to get a lot of transactions, it should work well by processing the page_no variable and offset variable in get_transaction_page ()
and looping.
-Also, the import of datetime was leaked, so I added it.
-In addition, tsv is output.get_tx_values.py(2019/2/7 version)
from etherscan import accounts
import pandas as pd
import datetime as dt
API_KEY = 'API_KEY' # change API_KEY
ADDRESS = '0xb794f5ea0ba39494ce839613fffba74279579268' # change address
def get_tx_value(page_no, address, api_key):
ac = accounts.Account(address=address,api_key=api_key)
# txes = ac.get_all_transactions()
txes = ac.get_transaction_page(page_no)
ret = []
for t in txes:
tmp = []
tmp.append(t.get('from'))
tmp.append(t.get('to'))
tmp.append(int(t.get('value')) / 1000000000000000000 )
tmp.append(dt.datetime.fromtimestamp(int(t.get('timeStamp'))))
ret.append(tmp)
return ret
ret = get_tx_value(page_no=1, address=ADDRESS,api_key=API_KEY)
df = pd.DataFrame(ret,columns=['from','to','value','datetime'])
df.to_csv('txes.tsv',sep='\t')
get_tx_values.py(old version. Does not work with error)
from etherscan import accounts
import pandas as pd
import datetime as dt
API_KEY = 'API_KEY' # change API_KEY
ADDRESS = '0xb794f5ea0ba39494ce839613fffba74279579268' # change address
def get_tx_value(address,api_key):
ac = accounts.Account(address=address,api_key=api_key)
txes = ac.get_all_transactions()
ret = []
for t in txes:
tmp = []
tmp.append(t.get('from'))
tmp.append(t.get('to'))
tmp.append(int(t.get('value')) / 1000000000000000000 )
tmp.append(dt.datetime.fromtimestamp(int(t.get('timeStamp'))))
ret.append(tmp)
return ret
ret = get_tx_value(address=ADDRESS,api_key=API_KEY)
df = pd.DataFrame(ret,columns=['from','to','value','datetime'])
result
from to value datetime
0 0x32be343b94f860124dc4fee278fdcbd38c102d88 0xb794f5ea0ba39494ce839613fffba74279579268 100.000000 2015-08-09 07:47:35
1 0xb794f5ea0ba39494ce839613fffba74279579268 0x32be343b94f860124dc4fee278fdcbd38c102d88 99.000000 2015-08-09 07:53:13
2 0x32be343b94f860124dc4fee278fdcbd38c102d88 0xb794f5ea0ba39494ce839613fffba74279579268 99.001192 2015-08-09 07:55:43
3 0x32be343b94f860124dc4fee278fdcbd38c102d88 0xb794f5ea0ba39494ce839613fffba74279579268 900.000000 2015-08-09 07:58:23
4 0x32be343b94f860124dc4fee278fdcbd38c102d88 0xb794f5ea0ba39494ce839613fffba74279579268 9000.000000 2015-08-09 07:59:15
It matches the transaction on Etherscan. (Since python outputs from the oldest one, it comes out in the reverse order of Etherscan)
Explorer's API seems to be useful when analyzing address transactions.