DEV Community

drake
drake

Posted on

查询BTC地址的账户余额和交易记录

import requests
import time

address = '1Kbzp6o1QncwApTKKePasqxvcZsxQFYvZo'
url = f"https://blockchain.info/rawaddr/{address}"
response = requests.get(url)
data = response.json()

# data = response.json()

# 输出地址的基本信息
total_received = data['total_received'] / 1e8  # 转换为 BTC
total_sent = data['total_sent'] / 1e8  # 转换为 BTC
final_balance = data['final_balance'] / 1e8  # 转换为 BTC

print(f"地址: {address}")
print(f"接收到的总金额: {total_received} BTC")
print(f"发送的总金额: {total_sent} BTC")
print(f"当前余额: {final_balance} BTC")
print("-" * 30)



# 输出交易记录
for tx in data['txs']:
    print(f"交易哈希: {tx['hash']}")
    date_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(tx['time']))
    print(f"时间: {date_str}")
    print(f"总金额: {tx['result'] / 1e8} BTC")
    print("-" * 30)

Enter fullscreen mode Exit fullscreen mode

Top comments (0)