繁体   English   中英

如何从 Python 中的`requests.Response` object 中获取数据?

[英]How to get the data out of `requests.Response` object in Python?

  • 我正在从加密货币交易所dydx下载数据。
  • The example URL of REST API endpoint that I am dealing with is https://api.dydx.exchange/v3/historical-funding/BTC-USD (You can check out the API document of the API endpoint as well)
  • 由于dydx 提供了 Python 库,因此我使用 Python 代码从上述 API 端点下载数据。 但是,我在下载数据后收到的答案只是<dydx3.helpers.requests.Response at 0x1880b5a02b0>
  • 要查看requests.Response object 的内容,我使用了.text方法。 但是 output 是一条错误消息,说AttributeError: 'Response' object has no attribute 'text'
  • .json()也不起作用,因为 output 说AttributeError: 'Response' object has no attribute 'json'
  • 下面提供了我使用的 Python 代码。
  • 如何使用 ZA7F5F35426B927382173Z 获得此 URL https://api.dydx.exchange/v3/historical-funding/BTC-USD的相同数据?
from dydx3.helpers.request_helpers import generate_query_path
from dydx3.helpers.requests import request

class Public(object):

    def __init__(
        self,
        host,
    ):
        self.host = host

    # ============ Request Helpers ============

    def _get(self, request_path, params={}):
        return request(
            generate_query_path(self.host + request_path, params),
            'get',
        )

    def _put(self, endpoint, data):
        return request(
            self.host + '/v3/' + endpoint,
            'put',
            {},
            data,
        )

    # ============ Requests ============

    def get_historical_funding(self, market, effective_before_or_at=None):
        '''
        Get historical funding for a market
        :param market: required
        :type market: str in list [
            "BTC-USD",
            "ETH-USD",
            "LINK-USD",
            ...
        ]
        :param effective_before_or_at: optional
        :type effective_before_or_at: str
        :returns: Array of historical funding for a specific market
        :raises: DydxAPIError
        '''
        uri = '/'.join(['/v3/historical-funding', market])
        return self._get(
            uri,
            {'effectiveBeforeOrAt': effective_before_or_at},
        )

client = Public(host="https://api.dydx.exchange")
data = client.get_historical_funding("BTC-USD")

data
>>> <dydx3.helpers.requests.Response at 0x1880b5a02b0>

data.text
>>> AttributeError: 'Response' object has no attribute 'text'

  • 我用host="https://api.dydx.exchange"初始化客户端,因为根据API 文档,主机是 ZDB974238714CA8DE634A7CE1D083A14Z 端点的基础 URL。

在此处输入图像描述

该库的源代码显示了两个返回的属性: dataheaders

dydx3库中使用的命名有点混乱, dydx3.helpers.requests不是requests模块。

dydx3.helpers.requests.Response object 只有两个属性:

  • 包含来自 API 响应的 HTTP 标headers的标头
  • 包含空dict或包含 API 返回的 json data的数据。

因此,您在代码中获取数据所需要做的就是:

from dydx3.helpers.request_helpers import generate_query_path
from dydx3.helpers.requests import request

class Public(object):

    def __init__(
        self,
        host,
    ):
        self.host = host

    # ============ Request Helpers ============

    def _get(self, request_path, params={}):
        return request(
            generate_query_path(self.host + request_path, params),
            'get',
        )

    def _put(self, endpoint, data):
        return request(
            self.host + '/v3/' + endpoint,
            'put',
            {},
            data,
        )

    # ============ Requests ============

    def get_historical_funding(self, market, effective_before_or_at=None):
        '''
        Get historical funding for a market
        :param market: required
        :type market: str in list [
            "BTC-USD",
            "ETH-USD",
            "LINK-USD",
            ...
        ]
        :param effective_before_or_at: optional
        :type effective_before_or_at: str
        :returns: Array of historical funding for a specific market
        :raises: DydxAPIError
        '''
        uri = '/'.join(['/v3/historical-funding', market])
        return self._get(
            uri,
            {'effectiveBeforeOrAt': effective_before_or_at},
        ).data  # CHANGE MADE HERE

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM