Search code examples
pythonjsonapigrequests

How to print same dictionary object from multiple urls with grequest?


I have a list of URLs that all use the same json structure. I am trying to pull specific dictionary objects from all of the URLs at once with grequest. I am able to do it with one URL, though I am using request:

import requests
import json

main_api = 'https://bittrex.com/api/v1.1/public/getorderbook?market=BTC-1ST&type=both&depth=50'

json_data = requests.get(main_api).json()    

Quantity = json_data['result']['buy'][0]['Quantity']
Rate = json_data['result']['buy'][0]['Rate']
Quantity_2 = json_data['result']['sell'][0]['Quantity']
Rate_2 = json_data['result']['sell'][0]['Rate']

print ("Buy")
print(Rate)
print(Quantity)
print ("")
print ("Sell")
print(Rate_2)
print(Quantity_2)

I want to be able to print what I printed above, for every URL. But I do not know where to begin. This is what I have so far:

import grequests
import json


urls = [
    'https://bittrex.com/api/v1.1/public/getorderbook?market=BTC-1ST&type=both&depth=50',
    'https://bittrex.com/api/v1.1/public/getorderbook?market=BTC-2GIVE&type=both&depth=50',
    'https://bittrex.com/api/v1.1/public/getorderbook?market=BTC-ABY&type=both&depth=50',
]


requests = (grequests.get(u) for u in urls)
responses = grequests.map(requests)

I thought it would be something like print(response.json(['result']['buy'][0]['Quantity'] for response in responses)) but that does not work at all, and python returns the following: print(responses.json(['result']['buy'][0]['Quantity'] for response in responses)) AttributeError: 'list' object has no attribute 'json'. I am very new to python, and coding in general, and I would appreciate any help.


Solution

  • Your responses variable is a list of Response objects. If you simple print the list with

    print(responses)
    

    it gives you

    [<Response [200]>, <Response [200]>, <Response [200]>]
    

    the brackets [] tell you that this is a list and it contains three Responseobjects.

    When you type responses.json(...) you are telling python to call the json() method on the list object. The list, however does not offer such a method, only the objects in the list have it.

    What you need to do is access an element in the list and call the json() method on this element. This done by specifying the position of the list element you want to access like this:

    print(responses[0].json()['result']['buy'][0]['Quantity'])
    

    This will access the first element in the responses list.

    Of course, it is not practical to access each list element individually if you want to output many items. That's why there are loops. Using a loop you can simply say: do this for each element in my list. This looks like this:

    for response in responses:
        print("Buy")
        print(response.json()['result']['buy'][0]['Quantity'])
        print(response.json()['result']['buy'][0]['Rate'])
        print("Sell")
        print(response.json()['result']['sell'][0]['Quantity'])
        print(response.json()['result']['sell'][0]['Rate'])
        print("----")
    

    The for-each-loop executes the indented lines of code for each element in the list. The current element is available in the response variable.