如何找到网站API提取图形数据?

发布于 2025-02-06 22:11:41 字数 320 浏览 2 评论 0原文

https://wwwww.okx.com/markets/markets/spot-data/spot-data/btc-btc--btc--btc- USDT 。我想使用R提取数据。尽管我在Stackoverlfow上找到了类似的帖子来做到这一点。我似乎找不到此网站的API。我检查了所有XHR文件,没有链接?这是否意味着隐藏?我只想要“ BTC保证金贷款比率”的API,

谢谢。

https://www.okx.com/markets/spot-data/btc-usdt . I'd like to extract the data using R. Although I found a similar post on stackoverlfow to do this. I can't seem to find the API for this website. I checked all the XHR files and there is no link? does that mean it's hidden? I only want the api for the " BTC Margin lending Ratio"

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌若浮生 2025-02-13 22:11:41

XHR请求将数据作为JSON获取以制作图形。解析并复制情节非常简单。

为了使其更有用,您可以将其包装在启动日期和(可选)结束日期的函数中,并返回带有日期和值列的数据框架:

get_BTC_loan_ratio <- function(from, to = as.character(as.Date(Sys.time()) - 1))
{
  from <- as.character(from)
  to <- as.character(to)
  base_url <- "https://www.okx.com/priapi/v5/rubik/stat/margin/loan-ratio?"
  from <- paste0("&begin=", from, "T00:00:00Z")
  to <- paste0("&end=", to, "T23:00:00Z")
  url <- paste0(base_url, from, to, "&period=1D&ccy=BTC")
  df <- setNames(as.data.frame(jsonlite::fromJSON(url)$data), c("Date", "BTC"))
  df$BTC <- as.numeric(df$BTC)
  df$Date <- as.POSIXct(as.numeric(df$Date)/1000, origin = "1970-01-01")
  df[order(df$Date), ]
}

例如,自1月1日以来获取每日值的数据框架我们可以做:

BTC <- get_BTC_loan_ratio(from = "2022-01-01")

head(BTC)
#>                    Date     BTC
#> 163 2022-01-01 16:00:00  2.4766
#> 162 2022-01-02 16:00:00  2.9682
#> 161 2022-01-03 16:00:00  5.1275
#> 160 2022-01-04 16:00:00  3.6736
#> 159 2022-01-05 16:00:00  3.5300
#> 158 2022-01-06 16:00:00 17.6896

我们可以这样绘制它:

library(ggplot2)

ggplot(BTC, aes(Date, BTC)) +
  geom_line(color = "deepskyblue4") +
  theme_light(base_size = 16) +
  scale_x_datetime(date_labels = "%d %b % %Y")

​nofollow noreferrer“> reprex软件包(v2.0.1)

The XHR requests fetch the data as json to make the graph. It's pretty straightforward to parse this and replicate the plot.

To make it more useful, you can wrap this in a function that takes a start date and (optional) end date and returns a data frame with a date and value column:

get_BTC_loan_ratio <- function(from, to = as.character(as.Date(Sys.time()) - 1))
{
  from <- as.character(from)
  to <- as.character(to)
  base_url <- "https://www.okx.com/priapi/v5/rubik/stat/margin/loan-ratio?"
  from <- paste0("&begin=", from, "T00:00:00Z")
  to <- paste0("&end=", to, "T23:00:00Z")
  url <- paste0(base_url, from, to, "&period=1D&ccy=BTC")
  df <- setNames(as.data.frame(jsonlite::fromJSON(url)$data), c("Date", "BTC"))
  df$BTC <- as.numeric(df$BTC)
  df$Date <- as.POSIXct(as.numeric(df$Date)/1000, origin = "1970-01-01")
  df[order(df$Date), ]
}

For example, to get a data frame of daily values since 1st January we can do:

BTC <- get_BTC_loan_ratio(from = "2022-01-01")

head(BTC)
#>                    Date     BTC
#> 163 2022-01-01 16:00:00  2.4766
#> 162 2022-01-02 16:00:00  2.9682
#> 161 2022-01-03 16:00:00  5.1275
#> 160 2022-01-04 16:00:00  3.6736
#> 159 2022-01-05 16:00:00  3.5300
#> 158 2022-01-06 16:00:00 17.6896

And we can plot it like this:

library(ggplot2)

ggplot(BTC, aes(Date, BTC)) +
  geom_line(color = "deepskyblue4") +
  theme_light(base_size = 16) +
  scale_x_datetime(date_labels = "%d %b % %Y")

enter image description here

Created on 2022-06-13 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文