OutlawBandit
OutlawBandit

Reputation: 67

Building URLs with concatinate

Trying to build a URL using the variables I've already setup and then a known part of the URL on the back end. The code will throw the following error:

TypeError: cannot concatenate 'str' and 'NoneType' objects

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)

Logically, having the variables there and then adding on the rest of the URL as a string makes since to me. Where am I going astray?

Upvotes: 0

Views: 28

Answers (1)

gmds
gmds

Reputation: 19905

The problem is that when bitcoin_address is None (not provided by user), your program still tries to concatenate it to a str, which definitely won't work.

To solve that, you could add some code that checks the result of parse_args and raises an error when that happens, such as this:

if args.address is None:
    raise ValueError('A bitcoin address must be provided.')

Separately, while your approach to string formatting is generally correct, you should consider moving away from C-style formatting and towards the format method, for example:

At the start of your script:

base_url = 'https://blockexplorer.com/api/addrs/{address}/txs?from={from}&to={to}'

And later in the function:

full_url = base_url.format(address=bitcoin_address,
                           from=from_number,
                           to=to_number)

Upvotes: 1

Related Questions