当前位置: 首页 > 工具软件 > web3.py > 使用案例 >

web3.py获取公有链数据

吕征
2023-12-01

1. geth同步本地数据,如果需要账户信息的数据,需要用full模式同步。由于以太坊曾经受到了攻击,2.4m起同步速度异常缓慢

2. 安装web3, pypiwin32(windows下,因为要读文件,所以必须要装这个)

3. 简单的example:

from web3.eth import Eth
from web3 import Web3, HTTPProvider, IPCProvider
w3 = Web3(IPCProvider())
eth = Eth(w3)
print(eth.getBlock(1234))
out: AttributeDict({'difficulty': 31164958655, 'extraData': HexBytes('0x476574682f76312e302e302d66633739643332642f6c696e75782f676f312e34'), 'gasLimit': 5000, 'gasUsed': 0, 'hash': HexBytes('0x624d6c50f4edff05693806953b211050ef3e674ed18b1a1a6e64352086006f9e'), 'logsBloom': HexBytes('0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'), 'miner': '0x9DFc0377058b7b9EB277421769b56Df1395705f0', 'mixHash': HexBytes('0xe81e2bba84ab5924bd484624473329fc3538e5848034e58095914a0b16e40c46'), 'nonce': HexBytes('0xcca41431b3aa19ba'), 'number': 1234, 'parentHash': HexBytes('0x516e2ff1ad39cad2535198e95e1db75761d2ae8b3c339fb748388eb0591349cd'), 'receiptsRoot': HexBytes('0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421'), 'sha3Uncles': HexBytes('0x8669cb4133a5fb545c3401a9c7b64a9ff791e9769185fadd1b5726afc3aa6655'), 'size': 1082, 'stateRoot': HexBytes('0x1d30299d2a6b35272df4bc4b8ce2a66a400f769b3086f441d637ca61d25f9c55'), 'timestamp': 1438272630, 'totalDifficulty': 28912979795736, 'transactions': [], 'transactionsRoot': HexBytes('0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421'), 'uncles': [HexBytes('0x429bcc1b81814807533e38d6ed1cc703412ad519429579b003235217e45b093c')]})
 类似资料: