如何使用 grequest 从多个 url 打印相同的字典对象?
How to print same dictionary object from multiple urls with grequest?
我有一个 URL 的列表,它们都使用相同的 json 结构。我正在尝试使用 grequest 一次从所有 URL 中提取特定的字典对象。我可以用一个 URL 来完成,尽管我使用的是请求:
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)
我希望能够为每个 URL 打印上面打印的内容。但我不知道从哪里开始。这是我目前所拥有的:
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)
我认为它会像 print(response.json(['result']['buy'][0]['Quantity'] for response in responses))
但那根本不起作用,python returns 以下内容:print(responses.json(['result']['buy'][0]['Quantity'] for response in responses)) AttributeError: 'list' object has no attribute 'json'
。我对 python 和一般编码还很陌生,如果有任何帮助,我将不胜感激。
您的 responses
变量是 Response
个对象的列表。如果您使用
简单地打印列表
print(responses)
它给你
[<Response [200]>, <Response [200]>, <Response [200]>]
括号 []
告诉您这是一个列表,它包含三个 Response
对象。
当您键入 responses.json(...)
时,您是在告诉 python 调用列表对象上的 json()
方法。然而,该列表不提供这样的方法,只有列表中的对象有它。
您需要做的是访问列表中的一个元素,然后对该元素调用 json()
方法。这通过指定您要访问的列表元素的位置来完成,如下所示:
print(responses[0].json()['result']['buy'][0]['Quantity'])
这将访问 responses
列表中的第一个元素。
当然,如果要输出很多项,单独访问每个列表元素是不切实际的。这就是为什么有循环。使用循环你可以简单地说:对我列表中的每个元素执行此操作。这看起来像这样:
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("----")
for-each-loop 为列表中的每个元素执行缩进的代码行。当前元素在 response
变量中可用。
我有一个 URL 的列表,它们都使用相同的 json 结构。我正在尝试使用 grequest 一次从所有 URL 中提取特定的字典对象。我可以用一个 URL 来完成,尽管我使用的是请求:
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)
我希望能够为每个 URL 打印上面打印的内容。但我不知道从哪里开始。这是我目前所拥有的:
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)
我认为它会像 print(response.json(['result']['buy'][0]['Quantity'] for response in responses))
但那根本不起作用,python returns 以下内容:print(responses.json(['result']['buy'][0]['Quantity'] for response in responses)) AttributeError: 'list' object has no attribute 'json'
。我对 python 和一般编码还很陌生,如果有任何帮助,我将不胜感激。
您的 responses
变量是 Response
个对象的列表。如果您使用
print(responses)
它给你
[<Response [200]>, <Response [200]>, <Response [200]>]
括号 []
告诉您这是一个列表,它包含三个 Response
对象。
当您键入 responses.json(...)
时,您是在告诉 python 调用列表对象上的 json()
方法。然而,该列表不提供这样的方法,只有列表中的对象有它。
您需要做的是访问列表中的一个元素,然后对该元素调用 json()
方法。这通过指定您要访问的列表元素的位置来完成,如下所示:
print(responses[0].json()['result']['buy'][0]['Quantity'])
这将访问 responses
列表中的第一个元素。
当然,如果要输出很多项,单独访问每个列表元素是不切实际的。这就是为什么有循环。使用循环你可以简单地说:对我列表中的每个元素执行此操作。这看起来像这样:
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("----")
for-each-loop 为列表中的每个元素执行缩进的代码行。当前元素在 response
变量中可用。