我對鏈接提供的資料感興趣:https ://stats.bis.org/api/v1/data/WS_XRU_D/D.RU../all?detail=full
到目前為止,我用于檢索每日匯率歷史資料的代碼已經發展成為這些基本行,而我似乎堅持意識到我無法提取我感興趣的核心每日資料:
u <- "https://stats.bis.org/api/v1/data/WS_XRU_D/D.RU../all?detail=full"
d <- xml2::read_xml(u)
d
{xml_document}
<StructureSpecificData
xmlns:ss="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/structurespecific
...
[1] <message:Header>\n <message:ID>IDREF85e8b4cf-d7d2-4506-81e6-adf668cd841b</message:ID>\n <message:Test>fa ...
[2] <message:DataSet ss:dataScope="DataStructure" xsi:type="ns1:DataSetType" ss:structureRef="BIS_WS_XRU_D_1_0 ...
對于如何正確進行 xml 資料檢索的任何建議,我將不勝感激!
uj5u.com熱心網友回復:
您從正確的軌道開始,只需提取正確的節點并獲取屬性值:
library(xml2)
library(dplyr)
#read the page
url <- "https://stats.bis.org/api/v1/data/WS_XRU_D/D.RU../all?detail=full"
page <- xml2::read_xml(url)
#extract the OBS nodes
#<Obs TIME_PERIOD="1992-07-01" OBS_VALUE="0.12526" OBS_STATUS="A" OBS_CONF="F"/>
observations <- xml_find_all(page, ".//Obs")
#extract the attribute vales from each node
date <- observations %>% xml_attr("TIME_PERIOD") %>% as.Date("%Y-%m-%d")
value <- observations %>% xml_attr("OBS_VALUE") %>% as.numeric()
status <- observations %>% xml_attr("OBS_STATUS")
conf <- observations %>% xml_attr("OBS_CONF")
answer <- data.frame(date, value, status, conf)
plot(answer$date, answer$value)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/532166.html
標籤:rxml数据检索
