DEV Community

drake
drake

Posted on • Edited on

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

import requests
import time

# address = '15aayQV7Dx6u22wLnR2fe8DVmuVraUQXQn'
address = 'bc1qa9sw3l5ucl9zse3sjmtmxh5zkzylaw0vp3xtky'
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("-" * 60)



# 输出交易记录
for tx in data['txs']:
    # print(tx)
    print(f"交易哈希: {tx['hash']}")
    output_addr = tx['out']
    if output_addr:
        output_addr = output_addr[0]['addr']
        print(f'转出接受地址:{output_addr}')
    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

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay