Python Forum

Full Version: Python3 requests.models.Response
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I'm trying to get information from every individual tag key like symbol, pair, markPrice, IndexPice, bid and so on. Does anybody can help me to iterate over this object to get each value please?

import requests
import json

req = requests.get('https://futures.kraken.com/derivates/api/v3/tickers')
print(type(req))
obj = req.json()
print(type(obj))

for k in obj:
    for x in obj[k]:
        print(x,k)
Output:
<class 'requests.models.Response'> <class 'dict'> {'tag': 'perpetual', 'pair': 'XBT:USD', 'symbol': 'pi_xbtusd', 'markPrice': 43529.8, 'bid': 43514.5, 'bidSize': 8976, 'ask': 43545, 'askSize': 5600, 'vol24h': 126116935, 'openInterest': 103754747.0, 'open24h': 43969, 'indexPrice': 43562.01, 'last': 43552.5, 'lastTime': '2022-02-17T04:00:30.649Z', 'lastSize': 4400, 'suspended': False, 'fundingRate': -4.86247209e-10, 'fundingRatePrediction': -2.3744941e-10} tickers {'tag': 'perpetual', 'pair': 'ETH:USD', 'symbol': 'pi_ethusd', 'markPrice': 3081.38, 'bid': 3080.1, 'bidSize': 7500, 'ask': 3082.6, 'askSize': 400, 'vol24h': 25550994, 'openInterest': 67951298.0, 'open24h': 3142, 'indexPrice': 3082.05, 'last': 3083.8, 'lastTime': '2022-02-17T04:00:28.705Z', 'lastSize': 1, 'suspended': False, 'fundingRate': -7.275179811e-09, 'fundingRatePrediction': 1.707476211e-09} tickers {'tag': 'perpetual', 'pair': 'LTC:USD', 'symbol': 'pi_ltcusd', 'markPrice': 126.17, 'bid': 126.1, 'bidSize': 2394, 'ask': 126.21, 'askSize': 3120, 'vol24h': 4713388, 'openInterest': 2337699.0, 'open24h': 129.47, 'indexPrice': 126.24, 'last': 126.15, 'lastTime': '2022-02-17T04:00:25.458Z', 'lastSize': 3067, 'suspended': False, 'fundingRate': -2.85598926868e-07, 'fundingRatePrediction': -4.30648001583e-07} tickers {'tag': 'perpetual', 'pair': 'BCH:USD', 'symbol': 'pi_bchusd', 'markPrice': 330, 'bid': 329.5, 'bidSize': 5077, 'ask': 330.4, 'askSize': 19866, 'vol24h': 633695, 'openInterest': 1175084.0, 'open24h': 335.8, 'indexPrice': 330.07, 'last': 330.4, 'lastTime': '2022-02-17T04:00:32.147Z', 'lastSize': 2, 'suspended': False, 'fundingRate': -1.53118887754e-07, 'fundingRatePrediction': -9.1671839046e-08} tickers {'tag': 'perpetual', 'pair': 'XRP:USD', 'symbol': 'pi_xrpusd', 'markPrice': 0.821, 'bid': 0.8204, 'bidSize': 5985, 'ask': 0.8215, 'askSize': 3040, 'vol24h': 11796871, 'openInterest': 3107929.0, 'open24h': 0.8358, 'indexPrice': 0.82186, 'last': 0.8223, 'lastTime': '2022-02-17T04:00:22.306Z', 'lastSize': 1940, 'suspended': False, 'fundingRate': 2.0826146090948e-05, 'fundingRatePrediction': -3.69892211193e-06} tickers {'tag': 'quarter', 'pair': 'XBT:USD', 'symbol': 'fi_xbtusd_220325', 'markPrice': 43532, 'bid': 43515, 'bidSize': 386, 'ask': 43557, 'askSize': 17809, 'vol24h': 3518394, 'openInterest': 10506598.0, 'open24h': 43925, 'indexPrice': 43562.01, 'last': 43510.5, 'lastTime': '2022-02-17T04:00:26.289Z', 'lastSize': 24635, 'suspended': False} tickers {'tag': 'quarter', 'pair': 'ETH:USD', 'symbol': 'fi_ethusd_220325', 'markPrice': 3083.3, 'bid': 3081.05, 'bidSize': 1171, 'ask': 3085.55, 'askSize': 1171, 'vol24h': 860315, 'openInterest': 10058907.0, 'open24h': 3139, 'indexPrice': 3082.05, 'last': 3088.35, 'lastTime': '2022-02-17T03:59:45.077Z', 'lastSize': 485, 'suspended': False} tickers {'tag': 'semiannual', 'pair': 'XBT:USD', 'symbol': 'fi_xbtusd_220624', 'markPrice': 44059.5, 'bid': 44024.5, 'bidSize': 1898, 'ask': 44091, 'askSize': 12278, 'vol24h': 3783572, 'openInterest': 10955825.0, 'open24h': 44465.5, 'indexPrice': 43562.01, 'last': 43999.5, 'lastTime': '2022-02-17T03:59:28.568Z', 'lastSize': 74985, 'suspended': False} tickers {'tag': 'semiannual', 'pair': 'ETH:USD', 'symbol': 'fi_ethusd_220624', 'markPrice': 3117.62, 'bid': 3114.35, 'bidSize': 1098, 'ask': 3120.8, 'askSize': 500, 'vol24h': 137105, 'openInterest': 2870782.0, 'open24h': 3165.2, 'indexPrice': 3082.05, 'last': 3137.1, 'lastTime': '2022-02-17T03:38:51.177Z', 'lastSize': 2822, 'suspended': False} tickers {'tag': 'quarter', 'pair': 'BCH:USD', 'symbol': 'fi_bchusd_220325', 'markPrice': 326.6, 'bid': 325.7, 'bidSize': 500, 'ask': 327.4, 'askSize': 500, 'vol24h': 9529, 'openInterest': 642708.0, 'open24h': 330.9, 'indexPrice': 330.07, 'last': 333.8, 'lastTime': '2022-02-16T19:40:06.147Z', 'lastSize': 1, 'suspended': False} tickers {'tag': 'quarter', 'pair': 'XRP:USD', 'symbol': 'fi_xrpusd_220325', 'markPrice': 0.8192, 'bid': 0.8182, 'bidSize': 611, 'ask': 0.8198, 'askSize': 611, 'vol24h': 194494, 'openInterest': 1037273.0, 'open24h': 0.8337, 'indexPrice': 0.82186, 'last': 0.8196, 'lastTime': '2022-02-17T04:00:18.137Z', 'lastSize': 100, 'suspended': False} tickers {'tag': 'quarter', 'pair': 'LTC:USD', 'symbol': 'fi_ltcusd_220325', 'markPrice': 125.82, 'bid': 125.7, 'bidSize': 8635, 'ask': 125.94, 'askSize': 691, 'vol24h': 331213, 'openInterest': 1342865.0, 'open24h': 129.18, 'indexPrice': 126.24, 'last': 125.89, 'lastTime': '2022-02-17T04:00:25.429Z', 'lastSize': 270, 'suspended': False} tickers {'tag': 'month', 'pair': 'ETH:USD', 'symbol': 'fi_ethusd_220225', 'markPrice': 3078.08, 'bid': 3075.35, 'bidSize': 1193, 'ask': 3080.75, 'askSize': 500, 'vol24h': 925828, 'openInterest': 323104.0, 'open24h': 3136.55, 'indexPrice': 3082.05, 'last': 3083.2, 'lastTime': '2022-02-17T03:59:45.072Z', 'lastSize': 433, 'suspended': False} tickers {'tag': 'month', 'pair': 'XBT:USD', 'symbol': 'fi_xbtusd_220225', 'markPrice': 43521.5, 'bid': 43485.5, 'bidSize': 1993, 'ask': 43548.5, 'askSize': 20000, 'vol24h': 1294515, 'openInterest': 2462745.0, 'open24h': 44004.5, 'indexPrice': 43562.01, 'last': 43500, 'lastTime': '2022-02-17T04:00:26.037Z', 'lastSize': 1000, 'suspended': False} tickers {'tag': 'month', 'pair': 'XRP:USD', 'symbol': 'fi_xrpusd_220225', 'markPrice': 0.8182, 'bid': 0.8175, 'bidSize': 633, 'ask': 0.8191, 'askSize': 633, 'vol24h': 254403, 'openInterest': 87723.0, 'open24h': 0.8326, 'indexPrice': 0.82186, 'last': 0.8197, 'lastTime': '2022-02-17T04:00:22.306Z', 'lastSize': 713, 'suspended': False} tickers {'tag': 'month', 'pair': 'BCH:USD', 'symbol': 'fi_bchusd_220225', 'markPrice': 328.8, 'bid': 328, 'bidSize': 500, 'ask': 329.7, 'askSize': 500, 'vol24h': 0, 'openInterest': 66712.0, 'indexPrice': 330.07, 'last': 335.7, 'lastTime': '2022-02-15T22:01:38.610Z', 'lastSize': 500, 'suspended': False} tickers {'tag': 'month', 'pair': 'LTC:USD', 'symbol': 'fi_ltcusd_220225', 'markPrice': 125.84, 'bid': 125.73, 'bidSize': 2925, 'ask': 125.93, 'askSize': 713, 'vol24h': 64124, 'openInterest': 122023.0, 'open24h': 129.14, 'indexPrice': 126.24, 'last': 127.24, 'lastTime': '2022-02-17T03:53:01.815Z', 'lastSize': 1, 'suspended': False} tickers {'symbol': 'in_xbtusd', 'last': 43562, 'lastTime': '2022-02-17T04:00:35.000Z'} tickers {'symbol': 'in_xrpusd', 'last': 0.8219, 'lastTime': '2022-02-17T04:00:35.000Z'} tickers {'symbol': 'in_ethusd', 'last': 3082.05, 'lastTime': '2022-02-17T04:00:35.000Z'} tickers {'symbol': 'in_ltcusd', 'last': 126.24, 'lastTime': '2022-02-17T04:00:35.000Z'} tickers {'symbol': 'in_bchusd', 'last': 330.07, 'lastTime': '2022-02-17T04:00:35.000Z'} tickers {'symbol': 'rr_xbtusd', 'last': 43642, 'lastTime': '2022-02-16T16:00:00.000Z'} tickers {'symbol': 'rr_xrpusd', 'last': 0.8177, 'lastTime': '2022-02-16T16:00:00.000Z'} tickers {'symbol': 'rr_ethusd', 'last': 3069.74, 'lastTime': '2022-02-16T16:00:00.000Z'} tickers {'symbol': 'rr_ltcusd', 'last': 127.43, 'lastTime': '2022-02-16T16:00:00.000Z'} tickers {'symbol': 'rr_bchusd', 'last': 331.13, 'lastTime': '2022-02-16T16:00:00.000Z'} tickers
Regards!
It could be better to post the output from print(obj)

Otherwise:

for ticker in obj['tickers']:
    for key, value in ticker.items():
        print(f'{key} --> {value}')
(Feb-17-2022, 05:18 AM)buran Wrote: [ -> ]It could be better to post the output from print(obj)

Otherwise:

for ticker in obj['tickers']:
    for key, value in ticker.items():
        print(f'{key} --> {value}')

Thanks buran!

The output from print(obj)is:

Output:
{'result': 'success', 'tickers': [{'tag': 'perpetual', 'pair': 'XBT:USD', 'symbol': 'pi_xbtusd', 'markPrice': 42024.5, 'bid': 42016, 'bidSize': 30023, 'ask': 42032.5, 'askSize': 249288, 'vol24h': 145802972, 'openInterest': 101840346.0, 'open24h': 43621.5, 'indexPrice': 41999.31, 'last': 42033.5, 'lastTime': '2022-02-17T15:32:32.597Z', 'lastSize': 166192, 'suspended': False, 'fundingRate': -6.127504e-10, 'fundingRatePrediction': -1.68117636e-10}, {'tag': 'perpetual', 'pair': 'ETH:USD', 'symbol': 'pi_ethusd', 'markPrice': 2987.92, 'bid': 2986.4, 'bidSize': 3600, 'ask': 2988.85, 'askSize': 150, 'vol24h': 36099944, 'openInterest': 66819345.0, 'open24h': 3064, 'indexPrice': 2986.2, 'last': 2986.95, 'lastTime': '2022-02-17T15:32:09.792Z', 'lastSize': 770, 'suspended': False, 'fundingRate': 6.98282888e-10, 'fundingRatePrediction': 3.604511726e-09}, {'tag': 'perpetual', 'pair': 'LTC:USD', 'symbol': 'pi_ltcusd', 'markPrice': 122.78, 'bid': 122.7, 'bidSize': 7981, 'ask': 122.86, 'askSize': 13098, 'vol24h': 5889343, 'openInterest': 2269363.0, 'open24h': 127.57, 'indexPrice': 122.79, 'last': 122.64, 'lastTime': '2022-02-17T15:30:45.311Z', 'lastSize': 7098, 'suspended': False, 'fundingRate': -2.8172630921e-07, 'fundingRatePrediction': -3.55297717899e-07}, {'tag': 'perpetual', 'pair': 'BCH:USD', 'symbol': 'pi_bchusd', 'markPrice': 323.2, 'bid': 322.9, 'bidSize': 6671, 'ask': 323.5, 'askSize': 48048, 'vol24h': 416220, 'openInterest': 1170155.0, 'open24h': 330.9, 'indexPrice': 323.15, 'last': 322.1, 'lastTime': '2022-02-17T15:21:13.181Z', 'lastSize': 1, 'suspended': False, 'fundingRate': -1.00742876647e-07, 'fundingRatePrediction': -2.08698730728e-07}, {'tag': 'perpetual', 'pair': 'XRP:USD', 'symbol': 'pi_xrpusd', 'markPrice': 0.802, 'bid': 0.8016, 'bidSize': 31564, 'ask': 0.8025, 'askSize': 16799, 'vol24h': 9488288, 'openInterest': 2328458.0, 'open24h': 0.8176, 'indexPrice': 0.80173, 'last': 0.8, 'lastTime': '2022-02-17T15:30:29.572Z', 'lastSize': 1874, 'suspended': False, 'fundingRate': 6.779454542298e-06, 'fundingRatePrediction': -3.4404755359337e-05}, {'tag': 'quarter', 'pair': 'XBT:USD', 'symbol': 'fi_xbtusd_220325', 'markPrice': 42007.5, 'bid': 41994, 'bidSize': 302, 'ask': 42027, 'askSize': 1971, 'vol24h': 4316691, 'openInterest': 10235601.0, 'open24h': 43679, 'indexPrice': 41999.31, 'last': 42014.5, 'lastTime': '2022-02-17T15:30:45.330Z', 'lastSize': 1971, 'suspended': False}, {'tag': 'quarter', 'pair': 'ETH:USD', 'symbol': 'fi_ethusd_220325', 'markPrice': 2988.88, 'bid': 2986.9, 'bidSize': 1671, 'ask': 2990.85, 'askSize': 1071, 'vol24h': 2173259, 'openInterest': 9938869.0, 'open24h': 3069.75, 'indexPrice': 2986.2, 'last': 2988.85, 'lastTime': '2022-02-17T15:31:33.850Z', 'lastSize': 100, 'suspended': False}, {'tag': 'semiannual', 'pair': 'XBT:USD', 'symbol': 'fi_xbtusd_220624', 'markPrice': 42468.5, 'bid': 42439, 'bidSize': 1898, 'ask': 42498, 'askSize': 47269, 'vol24h': 2572069, 'openInterest': 11089765.0, 'open24h': 44228.5, 'indexPrice': 41999.31, 'last': 42390, 'lastTime': '2022-02-17T15:27:36.117Z', 'lastSize': 500, 'suspended': False}, {'tag': 'semiannual', 'pair': 'ETH:USD', 'symbol': 'fi_ethusd_220624', 'markPrice': 3020.1, 'bid': 3016.6, 'bidSize': 1663, 'ask': 3023.15, 'askSize': 500, 'vol24h': 346425, 'openInterest': 2914572.0, 'open24h': 3104.2, 'indexPrice': 2986.2, 'last': 3024.45, 'lastTime': '2022-02-17T15:05:29.051Z', 'lastSize': 10, 'suspended': False}, {'tag': 'quarter', 'pair': 'BCH:USD', 'symbol': 'fi_bchusd_220325', 'markPrice': 319.8, 'bid': 319.3, 'bidSize': 501, 'ask': 320.4, 'askSize': 500, 'vol24h': 621, 'openInterest': 642106.0, 'open24h': 327.3, 'indexPrice': 323.15, 'last': 319.5, 'lastTime': '2022-02-17T15:20:10.479Z', 'lastSize': 1, 'suspended': False}, {'tag': 'quarter', 'pair': 'XRP:USD', 'symbol': 'fi_xrpusd_220325', 'markPrice': 0.8008, 'bid': 0.8, 'bidSize': 611, 'ask': 0.8012, 'askSize': 611, 'vol24h': 370539, 'openInterest': 1037047.0, 'open24h': 0.8188, 'indexPrice': 0.80173, 'last': 0.7986, 'lastTime': '2022-02-17T15:20:37.028Z', 'lastSize': 611, 'suspended': False}, {'tag': 'quarter', 'pair': 'LTC:USD', 'symbol': 'fi_ltcusd_220325', 'markPrice': 122.5, 'bid': 122.31, 'bidSize': 1111, 'ask': 122.57, 'askSize': 611, 'vol24h': 393338, 'openInterest': 1364600.0, 'open24h': 127.01, 'indexPrice': 122.79, 'last': 121.9, 'lastTime': '2022-02-17T15:18:38.226Z', 'lastSize': 1, 'suspended': False}, {'tag': 'month', 'pair': 'ETH:USD', 'symbol': 'fi_ethusd_220225', 'markPrice': 2983.1, 'bid': 2980.85, 'bidSize': 1694, 'ask': 2985.35, 'askSize': 1194, 'vol24h': 1219850, 'openInterest': 347944.0, 'open24h': 3060.4, 'indexPrice': 2986.2, 'last': 2978.25, 'lastTime': '2022-02-17T15:20:37.028Z', 'lastSize': 1194, 'suspended': False}, {'tag': 'month', 'pair': 'XBT:USD', 'symbol': 'fi_xbtusd_220225', 'markPrice': 41986, 'bid': 41952, 'bidSize': 2494, 'ask': 42020, 'askSize': 1994, 'vol24h': 2317565, 'openInterest': 2568550.0, 'open24h': 43601.5, 'indexPrice': 41999.31, 'last': 41948.5, 'lastTime': '2022-02-17T15:20:36.967Z', 'lastSize': 3600, 'suspended': False}, {'tag': 'month', 'pair': 'XRP:USD', 'symbol': 'fi_xrpusd_220225', 'markPrice': 0.8, 'bid': 0.7991, 'bidSize': 7687, 'ask': 0.8006, 'askSize': 634, 'vol24h': 404230, 'openInterest': 86828.0, 'open24h': 0.8155, 'indexPrice': 0.80173, 'last': 0.798, 'lastTime': '2022-02-17T15:20:37.047Z', 'lastSize': 58, 'suspended': False}, {'tag': 'month', 'pair': 'BCH:USD', 'symbol': 'fi_bchusd_220225', 'markPrice': 322, 'bid': 321.7, 'bidSize': 7342, 'ask': 322.4, 'askSize': 7540, 'vol24h': 4321, 'openInterest': 62718.0, 'open24h': 331.5, 'indexPrice': 323.15, 'last': 323.2, 'lastTime': '2022-02-17T14:24:25.935Z', 'lastSize': 1, 'suspended': False}, {'tag': 'month', 'pair': 'LTC:USD', 'symbol': 'fi_ltcusd_220225', 'markPrice': 122.48, 'bid': 122.36, 'bidSize': 634, 'ask': 122.62, 'askSize': 1134, 'vol24h': 82842, 'openInterest': 124517.0, 'open24h': 126.84, 'indexPrice': 122.79, 'last': 123.41, 'lastTime': '2022-02-17T14:16:00.232Z', 'lastSize': 144, 'suspended': False}, {'symbol': 'in_xbtusd', 'last': 41999, 'lastTime': '2022-02-17T15:32:32.000Z'}, {'symbol': 'in_xrpusd', 'last': 0.8017, 'lastTime': '2022-02-17T15:32:32.000Z'}, {'symbol': 'in_ethusd', 'last': 2986.2, 'lastTime': '2022-02-17T15:32:32.000Z'}, {'symbol': 'in_ltcusd', 'last': 122.79, 'lastTime': '2022-02-17T15:32:32.000Z'}, {'symbol': 'in_bchusd', 'last': 323.15, 'lastTime': '2022-02-17T15:32:32.000Z'}, {'symbol': 'rr_xbtusd', 'last': 43642, 'lastTime': '2022-02-16T16:00:00.000Z'}, {'symbol': 'rr_xrpusd', 'last': 0.8177, 'lastTime': '2022-02-16T16:00:00.000Z'}, {'symbol': 'rr_ethusd', 'last': 3069.74, 'lastTime': '2022-02-16T16:00:00.000Z'}, {'symbol': 'rr_ltcusd', 'last': 127.43, 'lastTime': '2022-02-16T16:00:00.000Z'}, {'symbol': 'rr_bchusd', 'last': 331.13, 'lastTime': '2022-02-16T16:00:00.000Z'}], 'serverTime': '2022-02-17T15:32:32.666Z'}
Is it possible to save all tag values in a variables?

Best regards!
(Feb-17-2022, 03:44 PM)ogautier Wrote: [ -> ]Is it possible to save all tag values in a variables?
This is not great idea. Iterate over obj['tickers'] then access the values you want by key and do whatever you want to do with them
(Feb-17-2022, 04:03 PM)buran Wrote: [ -> ]
(Feb-17-2022, 03:44 PM)ogautier Wrote: [ -> ]Is it possible to save all tag values in a variables?
This is not great idea. Iterate over obj['tickers'] then access the values you want by key and do whatever you want to do with them

Quote:Thank you very much buran!