赞
踩
- import json
- import time
- import requests
-
- def account_balance(account_address, api_key):
- # 设置API URL
- url = "https://api.etherscan.io/api"
-
- # 设置查询参数
- params = {
- "module": "account",
- "action": "balance",
- "address": f"{account_address}",
- "tag": "latest",
- "apikey": api_key # 将YourApiKeyToken替换为你的实际API密钥
- }
- # 发送GET请求
- response = requests.get(url, params=params)
-
- # 检查响应状态码
- if response.status_code == 200:
- # 解析JSON响应
- data = response.json()
-
- # 提取余额数据
- balance = data.get("result")
- # 将余额从Wei转换为Ether
- balance_wei = int(balance)
- balance_eth = balance_wei / 1e18 # 1 Ether = 1e18 Wei
- exchange_rate = 1582.53 # 美元汇率
- currency_balance = balance_eth * exchange_rate
-
- print(f"账户余额 虚拟货币: {balance_eth} ETH \n账户余额 真实货币: {currency_balance} \n汇率: {exchange_rate}")
- else:
- print(f"请求失败,状态码: {response.status_code}")
-
- if __name__ == "__main__":
-
- account_address = "xxx"
- etherscan_api_key = "xxx"
-
- account_balance(account_address, etherscan_api_key)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。