【自动化测试】web3py 连接 goerli

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

问题解决

资料

相关推荐
木西1 天前
深度拆解 Ether.fi 核心架构:当 Liquid Restaking 遇上 ERC-4337 与智能金库
web3·智能合约·solidity
明月_清风2 天前
Foundry Invariant Testing 实战:ERC-4626 + Handler + Ghost Variable
后端·web3·solidity
明月_清风2 天前
Foundry Invariant Testing:让测试自动寻找复杂状态下的 Solidity Bug
后端·web3·solidity
磐链科技2 天前
2026年9月Web3观察:机构入场加速,安全警钟长鸣
安全·web3·区块链
明月_清风3 天前
Foundry Cheatcode 详解:prank、warp、deal、expectRevert、expectEmit
后端·web3·solidity
明月_清风3 天前
Foundry Fuzz Testing:让测试自动寻找 Solidity Bug
后端·web3·solidity
明月_清风3 天前
Foundry Web3 测试框架入门:从 0 开始写 Solidity 测试
后端·web3·solidity
Web3李李4 天前
短剧+Web3:破解行业获客留客难题,打造零门槛用户增长闭环
web3·去中心化·区块链·软件开发·dapp开发·web3短剧·web3项目开发
磐链科技7 天前
交易所开发核心:钱包提现引擎的设计——从风控拦截、多级审批到自动广播上链
web3·区块链
磐链科技9 天前
Web3钱包开发中的交易风控体系:模拟执行、沙盒隔离与授权拦截
web3·区块链