如何将 Python GET 请求按参数拆分为不同的比特币报价

How to split Python GET request by parameter into different bitcoin offers

我想使用 BISQ API 提取 BTC 买入报价,并在新行中单独列出它们。但是,我得到的 return 级别太多,我无法使用键值对提取(尽管我对 Python 很陌生,所以可能遗漏了一些东西!)

目前,我给 API 打电话,回复如下:

import requests
import json
response_USD = requests.get('https://bisq.markets/api/offers?market=BTC_USD')
print(response_USD.json()['btc_usd']['buys'])

其中returns(因长度而删减):

[{'offer_id': 'aMDMHXt-9222c082-5f60-4274-9bbf-5d58d986d8b3-182', 'offer_date': 1645195105176, 'direction': 'BUY', 'min_amount': '0.01000000', 'amount': '0.02000000', 'price': '45000.00000000', 'volume': '900.00000000', 'payment_method': 'AMAZON_GIFT_CARD', 'offer_fee_txid': None}, {'offer_id': 'xf6xttjw-a21b0c58-4f46-487f-8f71-c33f20f0f61d-182', 'offer_date': 1644872308763, 'direction': 'BUY', 'min_amount': '0.00900000', 'amount': '0.01700000', 'price': '43255.79460000', 'volume': '735.34850000', 'payment_method': 'AMAZON_GIFT_CARD', 'offer_fee_txid': None}, {'offer_id': '82198-b1ab6e83-8a07-4a7c-8a25-00f91202c67f-180', 'offer_date': 1644131836719, 'direction': 'BUY', 'min_amount': '0.01130000', 'amount': '0.01130000', 'price': '42851.53480000', 'volume': '484.22230000', 'payment_method': 'AMAZON_GIFT_CARD', 'offer_fee_txid': None}, {'offer_id': '9y8ezr-0f9cb55a-0030-4610-b5af-72e7f19775ae-182', 'offer_date': 1644932335080, 'direction': 'BUY', 'min_amount': '0.01000000', 'amount': '0.02000000', 'price': '42851.53480000', 'volume': '857.03060000', 'payment_method': 'AMAZON_GIFT_CARD', 'offer_fee_txid': None}, {'offer_id': '856233-e943bc25-9df4-47e2-977f-6a65425fe45b-182', 'offer_date': 1644469262319, 'direction': 'BUY', 'min_amount': '0.00250000', 'amount': '0.00250000', 'price': '42447.27500000', 'volume': '106.11810000', 'payment_method': 'AMAZON_GIFT_CARD', 'offer_fee_txid': None}, {'offer_id': '318317-fb6d2475-f2dd-4b74-886e-7da6ff7b349c-182', 'offer_date': 1644122335075, 'direction': 'BUY', 'min_amount': '0.00310000', 'amount': '0.00310000', 'price': '42447.27500000', 'volume': '131.58650000', 'payment_method': 'AMAZON_GIFT_CARD', 'offer_fee_txid': None}, {'offer_id': '46MoyVB-daf83cf0-a064-4d32-8fd9-73593599bbb5-182', 'offer_date': 1645120327323, 'direction': 'BUY', 'min_amount': '0.01000000', 'amount': '0.01000000', 'price': '42043.01530000', 'volume': '420.43010000', 'payment_method': 'CLEAR_X_CHANGE', 'offer_fee_txid': None}...

我想提取每个报价(从 'offer_id' 到 'offer_fee_txid')作为一个新行,然后在该特定部分内调用(例如调用 'price')。

有简单的方法吗?

我建议迭代数据并复制出您需要的数据点如下:

import requests
import json

#request the raw dataset
response_USD = requests.get('https://bisq.markets/api/offers?market=BTC_USD')

#list of buys, and keys to extract
buys = []
extract = ["offer_id", "price"]

#iterate raw data, copying required datapoints
for buy_data in response_USD.json()['btc_usd']['buys']:
    buy = {}
    for key in extract:
        buy[key] = buy_data[key]
    buys.append(buy)

#print the collected data for each buy
for buy in buys:
    print(buy)

不确定确切地你需要什么,但这应该让你开始:

import requests

(r := requests.get('https://bisq.markets/api/offers?market=BTC_USD')).raise_for_status()
for buy in r.json()['btc_usd']['buys']:
    print(f"Offer id {buy['offer_id']} -> Price {buy['price']}")

输出(提取):

...
Offer id 8wqgbau-b2edf3d0-2f2b-4738-a956-e92227c906a1-182 -> Price 39277.97460000
Offer id ZEHFTYH-49190056-2aa8-4ea6-895f-39e1a1b22d89-182 -> Price 38877.17900000
Offer id XLKeI-0e6bbd7f-a947-4da1-8028-b8b9ef0a7308-175 -> Price 38877.17900000
Offer id 28444200-0b2719ca-544b-4c10-a6f3-0b811075e885-182 -> Price 38709.18190000
...