通过R停止工作从EIA的API下载数据的功能为什么?

时间:2015-08-07 19:54:45

标签: r

我试图找出为什么从EIA下载数据的功能停止工作的原因。为什么以下代码停止工作?

getWDEIA <- function(ID, key) {

ID <- unlist(strsplit(ID, ";"))
key <- unlist(strsplit(key, ";"))

url <- paste("http://api.eia.gov/series?series_id=", ID, "&api_key=", key, 
    "&out=xml", sep = "")

doc <- xmlParse(file = url, isURL = TRUE)

df <- xmlToDataFrame(nodes = getNodeSet(doc, "//data/row"))

df <- arrange(df, df$date)

date <- as.Date(df$date, "%Y%m%d")
values <- as.numeric(levels(df[, -1]))[df[, -1]]

xts_data <- xts(values, order.by = date)
names(xts_data) <- sapply(strsplit(ID, "-"), paste, collapse = ".")

assign(sapply(strsplit(ID, "-"), paste, collapse = "."), xts_data, envir = .GlobalEnv)
}

    getWDEIA(ID = "PET.W_EPC0_FPF_R48_MBBLD.4", key = key)

1 个答案:

答案 0 :(得分:1)

在此处发布问题之前,您似乎没有查看API中的原始XML。没有false_type XPath:

//data/row

您还没有包含2个必要的<?xml version="1.0" encoding="UTF-8"?> <eia_api> <request> <command>series</command> <series_id>PET.W_EPC0_FPF_R48_MBBLD.4</series_id> </request> <series> <row> <series_id>PET.W_EPC0_FPF_R48_MBBLD.4</series_id> <name>Lower 48 States Field Production of Crude Oil, 4 Week Avg</name> <units>Thousand Barrels per Day</units> <f>4</f> <unitsshort>Mbbl/d</unitsshort> <description>Lower 48 States Field Production of Crude Oil</description> <copyright>None</copyright> <source>EIA, U.S. Energy Information Administration</source> <start/> <end/> <updated>2015-08-05T13:14:06-0400</updated> <data/> </row> </series> </eia_api> 来电。下面的修改函数只返回正确定位路径中的数据框:

library

如果查看返回的数据框,看起来这绝对不是您期望的数据:

library(XML)
library(dplyr)

getWDEIA <- function(ID, key) {

  ID <- unlist(strsplit(ID, ";"))
  key <- unlist(strsplit(key, ";"))

  url <- paste("http://api.eia.gov/series?series_id=", ID, "&api_key=", key, 
               "&out=xml", sep = "")

  doc <- xmlParse(file = url, isURL = TRUE)

  df <- xmlToDataFrame(nodes = getNodeSet(doc, "//series/row"))

  return(df)

}

val <- getWDEIA(ID = "PET.W_EPC0_FPF_R48_MBBLD.4", key = "E7FDCCBA39FBA0268555B7E81D73CD47")

也许尝试另一个ID?