web3py 连接 goerli
直接使用库里方法
python
if __name__ == '__main__':
from web3.auto.infura.goerli import w3
w3.eth.get_balance(get_address_by_private_key(os.getenv("AAA_KEY")))
- error info:
text
websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 401
换一种方式
python
from web3 import Web3, HTTPProvider
connection = Web3(HTTPProvider('https://goerli.infura.io/v3/<API-KEY>'))
print ("Latest Ethereum block number", connection.eth.blockNumber)
但没有 api-key 的话
shell
Traceback (most recent call last):
File "/Users/apple/github/qadev-py-scripts/test-web3/spider_eth.py", line 26, in <module>
from web3.auto.infura.goerli import w3
File "/Users/apple/miniconda/envs/web3/lib/python3.7/site-packages/web3/auto/infura/__init__.py", line 13, in <module>
_infura_url = build_infura_url(INFURA_MAINNET_DOMAIN)
File "/Users/apple/miniconda/envs/web3/lib/python3.7/site-packages/web3/auto/infura/endpoints.py", line 55, in build_infura_url
key = load_api_key()
File "/Users/apple/miniconda/envs/web3/lib/python3.7/site-packages/web3/auto/infura/endpoints.py", line 35, in load_api_key
"No Infura Project ID found. Please ensure "
web3.exceptions.InfuraKeyNotFound: No Infura Project ID found. Please ensure that the environment variable WEB3_INFURA_PROJECT_ID is set.
从报错看的需要一个 WEB3_INFURA_PROJECT_ID
, 去申请一个 infura id
问题解决