web3.py 安装以及主币查询 - bsc链
Posted 我要买GTR45
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了web3.py 安装以及主币查询 - bsc链相关的知识,希望对你有一定的参考价值。
安装web3
pip install pip3
我的电脑会提示一个c++ 14.0的报错,按照以下步骤可以解决
error: Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft C++ Build Tools“
# 百度云链接:
https://pan.baidu.com/s/1MWcOigDWrohbWfKoWtmHJA
# 提取码:
6666
下载解压安装 Microsoft Visual C++ Build Tools 即可解决
BSC链主网设置
rpc link: https://bsc-dataseed1.binance.org/
连接主网
# 指定主网地址
rpc_link = "https://bsc-dataseed1.binance.org/"
# 创建链接
w3_rpc = Web3(Web3.HTTPProvider(rpc_link))
print("Main Net Connected" if w3_rpc.isConnected() else "Connect Failed")
获取最新区块下当前账户余额
# 获取最新区块账户余额
balance_data = w3_rpc.eth.get_balance(target_address)
余额进制转换
# 余额单位转换
balance = w3_rpc.fromWei(balance_data, "ether")
单位 | wei值 | Wei |
---|---|---|
wei | 1 | 1 |
Kwei (babbage) | 1e3 wei | 1,000 |
Mwei (lovelace) | 1e6 wei | 1,000,000 |
Gwei (shannon) | 1e9 wei | 1,000,000,000 |
microether (szabo) | 1e12 wei | 1,000,000,000,000 |
milliether (finney) | 1e15 wei | 1,000,000,000,000,000 |
ether | 1e18 wei | 1,000,000,000,000,000,000 |
完整代码
# -*- encoding: utf-8 -*-"
# @Time : 2021/12/25 18:34
# @Author : bibi45
# @Version : 1.0
# @Contact : bibi45gt@gmail.com
# @License : (C)Copyright 2019-2021, bibi45
# That’s weird… It worked yesterday.
from web3 import Web3
# 查询地址
target_address = "查询地址"
class Web3Connection:
def __init__(self):
# 指定主网地址
self.rpc_link = "https://bsc-dataseed1.binance.org/"
# 创建链接
self.w3_rpc = Web3(Web3.HTTPProvider(self.rpc_link))
print("Main Net Connected" if self.w3_rpc.isConnected() else "Connect Failed")
def check_balance(self, _target_address):
# 获取最新区块账户余额
balance_data = self.w3_rpc.eth.get_balance(_target_address)
# 余额转换
balance = self.w3_rpc.fromWei(balance_data, "ether")
print("latest balance: ".format(balance))
if __name__ == '__main__':
web3_obj = Web3Connection()
web3_obj.check_balance(target_address)
引用:
error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools" 错误解决方案引用:
作者:thao888
链接:https://www.jianshu.com/p/a74afe1b171f
以上是关于web3.py 安装以及主币查询 - bsc链的主要内容,如果未能解决你的问题,请参考以下文章