我正在尝试在python中使用web3。
我正在尝试遵循http://web3py.readthedocs.io/en/stable/web3.eth.html#web3.eth.Eth中的步骤
import web3
web3.eth.getBlock("1028201")
但是得到了AttributeError: module 'web3.eth' has no attribute 'getBlock'
。
我尝试了python3和python2.7,得到了相同的结果。
有什么建议吗?
谢谢
发布于 2018-01-10 22:49:40
在调用Web3
来设置web3.eth.getBlock
模块函数之前,请确保您正在实例化quickstart docs中提到的eth
对象。
from web3 import Web3, TestRPCProvider
w3 = Web3(TestRPCProvider())
看一看code for web3.eth,我们就会发现class Eth(Module):
包含def getBlock
。如果您还查看了definition of Module
,您会看到attach
函数用于实际重新定义具有所需行为的web3.eth
。通常在web3/main.py
中调用attach
函数
for module_name, module_class in modules.items():
module_class.attach(self, module_name)
注意,在上面的一个循环中,module_class
是Eth
,module_name
是"eth"
。
您可能遗漏了这个逻辑,因此请确保在调用web3.eth.getBlock
之前实例化了一个Web3
对象。
发布于 2018-12-06 12:27:31
我在以太坊网络工作。给定的代码对我来说很有效。
from web3 import Web3
w3 = Web3(Web3.HTTPProvider("https://ropsten.infura.io/"))
然后编写代码web3.eth.getBlock("1028201")
发布于 2021-06-24 16:19:15
可以使用web3.eth.get_block
应用编程接口按块的编号或散列查找块。块哈希应采用其十六进制表示形式。块号
按编号获取一个区块
web3.eth.get_block(12345)
参见文档。https://web3py.readthedocs.io/en/stable/examples.html#looking-up-blocks
https://stackoverflow.com/questions/48181275
复制相似问题