web3.py库使用

1、首先安装web3.py 的库

post image

安装好了、先来试试创建账号

import os
import csv
import web3
from web3 import Web3
#rpc 可以直接从chainlist.org 里寻找、
#我习惯使用blockpi.io里注册
# 初始化 Web3 实例
w3 = Web3(Web3.HTTPProvider('https://ethereum.blockpi.network/v1/rpc/public'))  # 使用你自己的 Infura 项目 ID

def create_eth_accounts(num_accounts):
    accounts = []

    for _ in range(num_accounts):
        # 创建一个新的以太坊账户
        account = w3.eth.account.create()

        # 获取公钥和私钥
        public_key = account.address
        private_key = account._private_key.hex()

        accounts.append((public_key, private_key))
    
    return accounts

def write_to_csv(accounts, csv_file):
    with open(csv_file, mode='w', newline='') as file:
        writer = csv.writer(file)
        writer.writerow(['Public Key', 'Private Key'])  # 写入表头

        for public_key, private_key in accounts:
            writer.writerow([public_key, private_key])

if __name__ == '__main__':
    num_accounts = 100  # 指定要创建的账户数量
    csv_file = 'ethereum_accounts.csv'  # 指定 CSV 文件名
    accounts = create_eth_accounts(num_accounts)
    write_to_csv(accounts, csv_file)
    print(f"成功创建了 {num_accounts} 个以太坊账户并将它们的公钥和私钥保存到 {csv_file} 文件中。")



点击运行、就可以看到一下就创建好的公钥、私钥。

妥善保存

post image