我有一个所有使用相同 json 结构的 URL 列表。我正在尝试使用 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 返回以下内容:print(responses.json(['result']['buy'][0]['Quantity'] for response in responses)) AttributeError: 'list' object has no attribute 'json'
. 我对 python 和一般的编码非常陌生,我将不胜感激。