Home > Net >  Timestring passed into URL to output JSON file - Python API Call
Timestring passed into URL to output JSON file - Python API Call

Time:11-30

I'm getting the following error for my python scraper:

import requests
import json

symbol_id = 'COINBASE_SPOT_BTC_USDT'
time_start = '2022-11-20T17:00:00'
time_end = '2022-11-21T05:00:00'
limit_levels = 100000000
limit = 100000000

url = 'https://rest.coinapi.io/v1/orderbooks/{symbol_id}/history?time_start={time_start}limit={limit}&limit_levels={limit_levels}'
headers = {'X-CoinAPI-Key' : 'XXXXXXXXXXXXXXXXXXXXXXX'}
response = requests.get(url, headers=headers)

print(response)

with open('raw_coinbase_ob_history.json', 'w') as json_file:
    json.dump(response.json(), json_file)

with open('raw_coinbase_ob_history.json', 'r') as handle:
    parsed = json.load(handle)
    with open('coinbase_ob_history.json', 'w') as coinbase_ob: 
        json.dump(parsed, coinbase_ob, indent = 4)
<Response [400]>

And in my written json file, I'm outputted

{"error": "Wrong format of 'time_start' parameter."}

I assume a string goes into a url, so I flattened the timestring to a string. I don't understand why this doesn't work. This is the documentation for the coinAPI call I'm trying to make with 'timestring'. https://docs.coinapi.io/?python#historical-data-get-4

CodePudding user response:

Incorrect syntax for python. To concatenate strings, stick them together like such:

a = 'a'   'b'   'c'

CodePudding user response:

string formatting is invalid, and also need use & in between different url params

# python3
url = f"https://rest.coinapi.io/v1/orderbooks/{symbol_id}/history?time_start={time_start}&limit={limit}&limit_levels={limit_levels}"

# python 2
url = "https://rest.coinapi.io/v1/orderbooks/{symbol_id}/history?time_start={time_start}&limit={limit}&limit_levels={limit_levels}".format(symbol_id=symbol_id, time_start=time_start, limit=limit, limit_levels=limit_levels)

https://docs.python.org/3/tutorial/inputoutput.html
https://docs.python.org/2/tutorial/inputoutput.html

  • Related