# web3.py库使用

By [Untitled](https://paragraph.com/@0x5b1445c2025e8a7a4f8609da3302993d8777ae4f) · 2023-11-20

---

1、首先安装web3.py 的库

![](https://storage.googleapis.com/papyrus_images/4ee80ffeddd87286a2645f922aedd6baeb495333877c75b9e89452e613877d12.png)

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

    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} 文件中。")
    

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

妥善保存

![](https://storage.googleapis.com/papyrus_images/f428da318f49015e840320341d1607d3c8dbfa9be886ae515b62de332d740b83.png)

---

*Originally published on [Untitled](https://paragraph.com/@0x5b1445c2025e8a7a4f8609da3302993d8777ae4f/web3-py)*
