简体   繁体   English

从 API [Python] 下载数据

[英]Download data from API [Python]

How can i download data from API, which look like this (sorry - maybe my informations are not clear but i have a beginer with API)我如何从 API 下载数据,看起来像这样(抱歉 - 也许我的信息不清楚,但我有一个 API 初学者)

access-control-allow-headers: Authorization,User-Agent,Range,X-Requested-With,Content-Type,Partner
access-control-allow-methods: GET, POST, OPTIONS
access-control-allow-origin: https://test.deribit.com
cache-control: no-store
connection: keep-alive
content-length: 149
content-type: application/json
date: Fri, 04 Sep 2020 08:36:46 GMT
server: nginx/1.17.9
vary: Origin,Authorization,Partner
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{
  "jsonrpc": "2.0",
  "id": 11,
  "error": {
    "message": "unauthorized",
    "code": 13009
  },
  "testnet": true,
  "usIn": 1599208606258957,
  "usOut": 1599208606259032,
  "usDiff": 75
}

i tried to use sth like this:我试着像这样使用某事:

import requests

payload = {
"jsonrpc": "2.0",
"id": 11,
"error": {
"message": "unauthorized",
"code": 13009
  },
"testnet": 'true',
"usIn": 1599208606258957,
"usOut": 1599208606259032,
"usDiff": 75
}
r=requests.get('https://test.deribit.com',data = payload)


r.json()

But i got an error.但我有一个错误。 Can u help me with this problem?你能帮我解决这个问题吗?

@Andy_101 yes, my error message is below: @Andy_101 是的,我的错误信息如下:

JSONDecodeError                           Traceback (most recent call last)
<ipython-input-50-33c5e77ebc90> in <module>()
     16 
     17 
---> 18 r.json()

~\Anaconda3\lib\site-packages\requests\models.py in json(self, **kwargs)
    894                     # used.
    895                     pass
--> 896         return complexjson.loads(self.text, **kwargs)
    897 
    898     @property

~\Anaconda3\lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    346             parse_int is None and parse_float is None and
    347             parse_constant is None and object_pairs_hook is None and not kw):
--> 348         return _default_decoder.decode(s)
    349     if cls is None:
    350         cls = JSONDecoder

~\Anaconda3\lib\json\decoder.py in decode(self, s, _w)
    335 
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

~\Anaconda3\lib\json\decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I think what you need is this.我想你需要的是这个。 Happy coding :) If you have any questions, leave me a comment, then I will help you of the best of my ability :)快乐编码 :) 如果您有任何问题,请给我留言,然后我会尽我所能帮助您 :)

Python API Tutorial Python API 教程

You've received an empty response since JSON is unable to serialize it, probably HTTP status 400. You can check response code using response.status_code , that might help you find the cause of the error.您收到了一个空响应,因为 JSON 无法对其进行序列化,可能是 HTTP 状态 400。您可以使用response.status_code检查响应代码,这可能会帮助您找到错误的原因。

From your question, I can only assume you need to call POST (or maybe PUT) instead of GET, since you are sending payload, which is not standard for GET requests:根据您的问题,我只能假设您需要调用 POST(或者可能是 PUT)而不是 GET,因为您正在发送有效负载,这不是 GET 请求的标准:

r=requests.post('https://test.deribit.com', data=payload)

Request body (your payload) is standard for POST (create an object using data from body), PUT/PATCH (Update object using data from request body).请求正文(您的有效负载)是 POST(使用来自正文的数据创建对象)、PUT/PATCH(使用来自请求正文的数据更新对象)的标准。 Post is sometimes also used to retrieve data, if there are too many parameters to fit in url. Post 有时也用于检索数据,如果 url 中的参数太多。

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

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