从 coinmarketcap 中抓取历史数据
Scraping historical data from coinmarketcap
我通常不会为从网络上抓取 table 而苦苦挣扎,但出于某种原因,当我尝试从以下页面抓取历史数据时,我无法 select想要 table.
这是 link 和我的代码
library(tidyverse)
library(rvest)
url <-read_html("https://coinmarketcap.com/currencies/bitcoin/historical-data/")
table <- url %>%
html_table() %>% .[[1]] %>% as.data.frame()
谢谢
数据很好地隐藏在页面的 script
元素中。它通过 JavaScript 动态加载到 table
中,这就是您找不到它的原因。
以下从 script
元素(ID __NEXT_DATA__
)中提取数据。
library(tidyverse)
library(rvest)
url <-read_html("https://coinmarketcap.com/currencies/bitcoin/historical-data/")
table <- url %>%
html_node("#__NEXT_DATA__") %>%
html_text() %>%
jsonlite::fromJSON()
table$props$initialState$cryptocurrency$ohlcvHistorical[[1]]$quotes
这给出了
time_open time_close time_high time_low quote.USD.open quote.USD.high
1 2020-10-10T00:00:00.000Z 2020-10-10T23:59:59.999Z 2020-10-10T03:16:44.000Z 2020-10-10T00:01:41.000Z 11059.14 11442.21
2 2020-10-11T00:00:00.000Z 2020-10-11T23:59:59.999Z 2020-10-11T15:31:43.000Z 2020-10-11T00:52:06.000Z 11296.08 11428.81
...
我通常不会为从网络上抓取 table 而苦苦挣扎,但出于某种原因,当我尝试从以下页面抓取历史数据时,我无法 select想要 table.
这是 link 和我的代码
library(tidyverse)
library(rvest)
url <-read_html("https://coinmarketcap.com/currencies/bitcoin/historical-data/")
table <- url %>%
html_table() %>% .[[1]] %>% as.data.frame()
谢谢
数据很好地隐藏在页面的 script
元素中。它通过 JavaScript 动态加载到 table
中,这就是您找不到它的原因。
以下从 script
元素(ID __NEXT_DATA__
)中提取数据。
library(tidyverse)
library(rvest)
url <-read_html("https://coinmarketcap.com/currencies/bitcoin/historical-data/")
table <- url %>%
html_node("#__NEXT_DATA__") %>%
html_text() %>%
jsonlite::fromJSON()
table$props$initialState$cryptocurrency$ohlcvHistorical[[1]]$quotes
这给出了
time_open time_close time_high time_low quote.USD.open quote.USD.high
1 2020-10-10T00:00:00.000Z 2020-10-10T23:59:59.999Z 2020-10-10T03:16:44.000Z 2020-10-10T00:01:41.000Z 11059.14 11442.21
2 2020-10-11T00:00:00.000Z 2020-10-11T23:59:59.999Z 2020-10-11T15:31:43.000Z 2020-10-11T00:52:06.000Z 11296.08 11428.81
...