尝试使用我已经设置的变量来构建URL,然后在后端使用URL的已知部分。该代码将引发以下错误:
TypeError:无法连接“ str”和“ NoneType”对象
block_explorer_url = "https://blockexplorer.com/api/addrs/"
#?from=0&to=50
parser = argparse.ArgumentParser(description='Collect and visualize Bitcoin transactions and any related hidden services.')
parser.add_argument("--graph",help="Output filename of the graph file. Example: bitcoin.gexf",default="bitcoingraph.gexf")
parser.add_argument("--address", help="A bitcoin address to begin the search on.",)
args = parser.parse_args()
bitcoin_address = args.address
graph_file = args.graph
#
# Retrieve all bitcoin transactions for a Bitcoin address
#
def get_all_transactions(bitcoin_address):
transactions = []
from_number = 0
to_number = 50
block_explorer_url_full = block_explorer_url + bitcoin_address + "/txs?from=%d&to=%d" % (from_number,to_number)
从逻辑上讲,在那里拥有变量,然后以字符串的形式添加URL的其余部分,这对我来说很有意义。我要迷路了?
答案 0 :(得分:1)
问题在于,当bitcoin_address
为None
(用户未提供)时,您的程序仍会尝试将其连接到str
,这肯定行不通。
要解决此问题,您可以添加一些代码来检查parse_args
的结果,并在发生这种情况时引发错误,例如:
if args.address is None:
raise ValueError('A bitcoin address must be provided.')
另外,虽然您的字符串格式设置方法通常是正确的,但您应该考虑从C样式格式转向format
方法,例如:
在脚本开头:
base_url = 'https://blockexplorer.com/api/addrs/{address}/txs?from={from}&to={to}'
然后在函数中:
full_url = base_url.format(address=bitcoin_address,
from=from_number,
to=to_number)