如何使用python打印etherscan API输出的特定值

问题描述 投票:0回答:1

我使用以下代码从 etherscan api 请求数据

from etherscan import Etherscan
eth = Etherscan("API_KEY_HERE") # key in quotation marks


response = eth.get_normal_txs_by_address(address="0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a", startblock="8200000", endblock="18998125", sort="srt")
print(response)

输出:

[{'blockNumber': '8202023', 'timeStamp': '1563819581', 'hash': '0x5afb8990f9ba7ecb7cb9ccc7c5522177f6d5e321ca18f070378ba416a71d8284', 'nonce': '62', 'blockHash': '0x6cd95e6b24fa9c5d8e33839890f379d8b91cad490b2639570f16fa06f58eaa3e', 'transactionIndex': '60', 'from': '0x19c16bfadf1e2412ec98afa694ee71a0ddcd309c', 'to': '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a', 'value': '1000000000000000', 'gas': '21000', 'gasPrice': '4800000000', 'isError': '0', 'txreceipt_status': '1', 'input': '0x', 'contractAddress': '', 'cumulativeGasUsed': '3894335', 'gasUsed': '21000', 'confirmations': '10796195', 'methodId': '0x', 'functionName': ''}, {'blockNumber': '11616713', 'timeStamp': '1610143998', 'hash': '0xbd633ba440d70d9132f17c9e34a64f9c0cb1fea014d927ae2fdcb2199de01c70', 'nonce': '23', 'blockHash': '0x5c5b750db80cf35e8fdc063503c4856824da5be440314ce9c98332f8edde33b7', 'transactionIndex': '236', 'from': '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a', 'to': '0xd26114cd6ee289accf82350c8d8487fedb8a0c07', 'value': '0', 'gas': '52346', 'gasPrice': '90000000000', 'isError': '0', 'txreceipt_status': '1', 'input': '0xa9059cbb000000000000000000000000c88fe66bcc71cf840c6a9aae4892c07a38b9e6260000000000000000000000000000000000000000000000a69671228c57640000', 'contractAddress': '', 'cumulativeGasUsed': '10008235', 'gasUsed': '52346', 'confirmations': '7381505', 'methodId': '0xa9059cbb', 'functionName': 'transfer(address _to, uint256 _value)'}, {'blockNumber': '15559109', 'timeStamp': '1663488083', 'hash': '0x99d9d995301ed292388e345a49b307e9442b438337f3ab89a50256d9f74aea80', 'nonce': '21', 'blockHash': '0x6c8e678ee9310aed9cefb63bdf218b7bb28ed6ab3ae6659e1991491f18547cc7', 'transactionIndex': '131', 'from': '0xcc60d9a9555d5116a7beb059f5aac9524ece2981', 'to': '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a', 'value': '999', 'gas': '21000', 'gasPrice': '3183407632', 'isError': '0', 'txreceipt_status': '1', 'input': '0x', 'contractAddress': '', 'cumulativeGasUsed': '15304890', 'gasUsed': '21000', 'confirmations': '3439109', 'methodId': '0x', 'functionName': ''}]

我只想打印last交易的时间戳。在本例中,有 3 笔交易。但可能有数千个。

预期输出:

1663488083
python json api output text-processing
1个回答
0
投票

将输出分配给变量 my_list

 my_list = [{'blockNumber': '8202023', 'timeStamp': '1563819581', 'hash': '0x5afb8990f9ba7ecb7cb9ccc7c5522177f6d5e321ca18f070378ba416a71d8284', 'nonce': '62', 'blockHash': '0x6cd95e6b24fa9c5d8e33839890f379d8b91cad490b2639570f16fa06f58eaa3e', 'transactionIndex': '60', 'from': '0x19c16bfadf1e2412ec98afa694ee71a0ddcd309c', 'to': '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a', 'value': '1000000000000000', 'gas': '21000', 'gasPrice': '4800000000', 'isError': '0', 'txreceipt_status': '1', 'input': '0x', 'contractAddress': '', 'cumulativeGasUsed': '3894335', 'gasUsed': '21000', 'confirmations': '10796195', 'methodId': '0x', 'functionName': ''}, {'blockNumber': '11616713', 'timeStamp': '1610143998', 'hash': '0xbd633ba440d70d9132f17c9e34a64f9c0cb1fea014d927ae2fdcb2199de01c70', 'nonce': '23', 'blockHash': '0x5c5b750db80cf35e8fdc063503c4856824da5be440314ce9c98332f8edde33b7', 'transactionIndex': '236', 'from': '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a', 'to': '0xd26114cd6ee289accf82350c8d8487fedb8a0c07', 'value': '0', 'gas': '52346', 'gasPrice': '90000000000', 'isError': '0', 'txreceipt_status': '1', 'input': '0xa9059cbb000000000000000000000000c88fe66bcc71cf840c6a9aae4892c07a38b9e6260000000000000000000000000000000000000000000000a69671228c57640000', 'contractAddress': '', 'cumulativeGasUsed': '10008235', 'gasUsed': '52346', 'confirmations': '7381505', 'methodId': '0xa9059cbb', 'functionName': 'transfer(address _to, uint256 _value)'}, {'blockNumber': '15559109', 'timeStamp': '1663488083', 'hash': '0x99d9d995301ed292388e345a49b307e9442b438337f3ab89a50256d9f74aea80', 'nonce': '21', 'blockHash': '0x6c8e678ee9310aed9cefb63bdf218b7bb28ed6ab3ae6659e1991491f18547cc7', 'transactionIndex': '131', 'from': '0xcc60d9a9555d5116a7beb059f5aac9524ece2981', 'to': '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a', 'value': '999', 'gas': '21000', 'gasPrice': '3183407632', 'isError': '0', 'txreceipt_status': '1', 'input': '0x', 'contractAddress': '', 'cumulativeGasUsed': '15304890', 'gasUsed': '21000', 'confirmations': '3439109', 'methodId': '0x', 'functionName': ''}]

然后获取列表中的最后一项并将其分配给des_ind:

des_ind = my_list[-1]
然后你可以从字典中访问最后一个时间戳 `print(des_ind['时间戳'])

© www.soinside.com 2019 - 2024. All rights reserved.