如何用Python提取Etherscan API输出中最后一笔交易的时间戳
需求说明
使用Etherscan API获取指定地址的普通交易列表后,仅打印最后一笔交易的timeStamp字段值。
实现代码
from etherscan import Etherscan eth = Etherscan("API_KEY_HERE") # 请替换为你的Etherscan API密钥 # 请求交易数据 response = eth.get_normal_txs_by_address( address="0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a", startblock="8200000", endblock="18998125", sort="desc" # 可选:用desc按区块降序排列,此时最后一笔为最新交易;若用asc则最后一笔为最早交易 ) # 提取并打印最后一笔交易的时间戳 if response: print(response[-1]['timeStamp'])
代码说明
- 交易列表
response是字典组成的数组,response[-1]可直接获取数组的最后一个元素(即最后一笔交易) - 通过字典键
'timeStamp'提取对应的时间戳字符串 - 增加非空判断,避免无交易返回时触发索引错误
内容的提问来源于stack exchange,提问作者pwrsheller
相关产品推荐
相关产品推荐

