简体   繁体   English

将 curl 请求转换为 python

[英]converting curl request to python

Im trying to turn this curl request into a python code.我试图将此 curl 请求转换为 python 代码。 I want to eventually be able to save this to a csv but I need to get connected first.我希望最终能够将其保存到 csv 但我需要先连接。

curl --compressed -H 'Accept: application/json' -H 'X-Api-Key: 123abc' 'https://us.market-api.kaiko.io/v2/data/trades.v1/exchanges/cbse/spot/btc-usd/aggregations/count_ohlcv_vwap?interval=1h'

I started with this:我从这个开始:

import requests
import json

key='api-key'

url = 'https://us.market-api.kaiko.io/v2/data/trades.v1/exchanges/'
s = requests.Session()
s.auth = (key)

headers = {
   *not sure how to do this*
}
r = requests.get(url, headers=headers)

the docs say this needs to be in the header:文档说这需要在 header 中:

Accept: application/json Accept-Encoding: gzip:接受:application/json 接受编码:gzip:

How do I include the api key?如何包含 api 密钥? how do I save the data once its returned?数据返回后如何保存?

X-Api-Key would be a request header, so you can include it in your headers variable, like this: X-Api-Key将是一个请求 header,因此您可以将其包含在 headers 变量中,如下所示:

headers = {
   "X-Api-Key": key,
   "Accept": "application/json",
   "Accept-Encoding": "gzip"
}

(took the others ones from your current curl request) (从您当前的 curl 请求中获取其他请求)

You can get the data by using r.text , like this:您可以使用r.text获取数据,如下所示:

print(r.text)

Your code should look like this:您的代码应如下所示:

import requests
import json

key='api-key'

url = 'https://us.market-api.kaiko.io/v2/data/trades.v1/exchanges/'

headers = {
   "X-Api-Key": key,
   "Accept": "application/json",
   "Accept-Encoding": "gzip"
}

r = requests.get(url, headers=headers)
print(r.text)

If you want to get a json object instead, you can use r.json()如果你想得到一个 json object 代替,你可以使用r.json()

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

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