【问题标题】:Bitcoin Transaction Mapping Throws KeyError比特币交易映射引发 KeyError
【发布时间】:2020-07-16 08:19:53
【问题描述】:

我有以下一段代码,它似乎一直运行到第 36 行 recipientlist.append(target["addr"]) 然后抛出错误 KeyError: 'addr'

但是“地址”似乎在数据中,所以不确定是什么问题

有人可以帮忙吗?

import json
import requests

z = 0
i = 0
firstpart = "https://blockchain.info/rawaddr/"
initialinput = '3PaGEcGDjPsNQHAQ4pTmjQuLXWoEwvnr11'
initialreq = firstpart + initialinput

firstjson = (requests.get(initialreq)).json()
graphvizlines = []

addresslist = []
usedaddresslist = []

addresslist.append(initialinput)
usedaddresslist.append(initialinput)

while i < 6:
    if z is 1:
        initialreq = firstpart + addresslist[i]
        firstjson = (requests.get(initialreq)).json()
    
    for transaction in firstjson["txs"]:
        payerlist = []
        recipientlist = []
        
        print("\n" + transaction["hash"])

        for item in transaction["inputs"]:
            payerlist.append(item["prev_out"]["addr"])
            if item["prev_out"]["addr"] not in addresslist:
                addresslist.append(item["prev_out"]["addr"])

        for target in transaction["out"]:
            recipientlist.append(target["addr"])
            if target["addr"] not in addresslist:
                addresslist.append(target["addr"])

        for payer in payerlist:
            for recipient in recipientlist:
                a = '"' + payer + '"' + " -> " + '"' + recipient + '"' + ";"
                if a not in graphvizlines:
                    graphvizlines.append(a)
    i = i + 1    
    z = 1
        

for t in graphvizlines:
    print(t)

【问题讨论】:

    标签: python python-3.x bitcoin


    【解决方案1】:

    虽然addr 存在于您的数据中,但它并非存在于每个inputs 元素中。检查txs 中的最后一个元素,您会看到inputs 是:

    "inputs": [
        {
            "sequence": 0,
            "witness": "304402203f872bfd7093fcdad6a3735cbd76f276279890b0304e6f23f54c51388cc2a84402203731d7a7f71265f072f6792c8f4d2e805ff8f86bbfbd0b48a187d573c051593001",
            "prev_out": {
                "spent": true,
                "spending_outpoints": [
                    {
                        "tx_index": 0,
                        "n": 0
                    }
                ],
                "tx_index": 0,
                "type": 0,
                "value": 1880609,
                "n": 1,
                "script": "0014292738ed3f9466f8eedd8c49e5bb013088a7052b"
            },
            "script": ""
        }
    ],
    

    此元素缺少prev_out.addr

    您需要首先检查 addr 元素是否存在或将循环包装在 try/except 中。

    for transaction in firstjson['txs']:
        ...
        for item in transaction['inputs']:
            address = item.get('prev_out').get('addr')
            if(address == None):
                continue
            payerlist.append(address)
            ...
    

    如果prev_out 不存在,上述操作仍然会失败,因此您应该确认结果中是什么以及可能是什么。

    【讨论】:

      猜你喜欢
      • 2017-05-31
      • 2014-01-22
      • 1970-01-01
      • 1970-01-01
      • 2021-04-15
      • 2017-03-11
      • 2016-01-09
      • 2012-05-31
      • 2012-02-02
      相关资源
      最近更新 更多