R 从中央银行网页下载经济官方数据

R 从中央银行网页下载经济官方数据,r,dataframe,R,Dataframe,我一直在寻找我的问题的答案。我读了,和其他一些相关,但我还是没有得到答案 我的问题很简单(我希望是),但答案不是(至少对我自己来说),我想从网络上导入一些经济数据,这是每个月衡量尼加拉瓜经济活动的指标,到目前为止,我尝试了以下方法: library(XML) u <- "http://www.bcn.gob.ni/estadisticas/trimestrales_y_mensuales/siec/datos/4.IMAE.htm" u <- htmlParse(u,encoding

我一直在寻找我的问题的答案。我读了,和其他一些相关,但我还是没有得到答案

我的问题很简单(我希望是),但答案不是(至少对我自己来说),我想从网络上导入一些经济数据,这是每个月衡量尼加拉瓜经济活动的指标,到目前为止,我尝试了以下方法:

library(XML)
u <- "http://www.bcn.gob.ni/estadisticas/trimestrales_y_mensuales/siec/datos/4.IMAE.htm"
u <- htmlParse(u,encoding="UTF-8")
imae <- readHTMLTable(doc=u, header=T)
imae

library(httr)
u2 <- "http://www.bcn.gob.ni/estadisticas/trimestrales_y_mensuales/siec/datos/4.IMAE.htm"
page <- GET(u2, user_agent("httr"))
x <- readHTMLTable(text_content(page), as.data.frame=TRUE)
库(XML)

u如果表的结构不像链接到的其他响应中那样好,那么这是一项有点麻烦的工作。但如果格式不变,它实际上更像是一次性的,但要小心——可能有风险。人们可以添加更多通用的解决方案

require(RCurl)
require(XML)
u <- "http://www.bcn.gob.ni/estadisticas/trimestrales_y_mensuales/siec/datos/4.IMAE.htm"

webpage <- getURL(u)
lines <- readLines(tc <- textConnection(webpage)); close(tc)
pagetree <- htmlTreeParse(lines, error=function(...){}, useInternalNodes = TRUE)

# parse tree by any tables
x <- xpathSApply(pagetree, "//*/table", xmlValue)  

# remove white space and such w/ regexes
unlisted <- unlist(strsplit(x, "\n"))
notabs <- gsub("\t","",unlisted)
nowhitespace <- sub("^[[:space:]]*(.*?)[[:space:]]*$", "\\1", notabs, perl=TRUE)
data <- nowhitespace[!(nowhitespace %in% c("", "|"))]
require(RCurl)
需要(XML)

正如我在评论中提到的,这个问题很可能是由于编码错误的表引起的

您可以尝试以下方法(在Ubuntu上使用RStudio进行测试)。它要求您拥有并安装了。如果您不想安装这些有用的程序,请跳到本答案的更新部分

  • 下载页面并“整理”它

  • 像往常一样继续进行R

    library(XML)
    u <- htmlParse("new.html")
    imae <- readHTMLTable(u)
    
  • 更新:一个小功能来帮助你 如果您可以忍受必须对重音字符进行一些文本清理,W3C提供了。这允许您编写如下所示的基本函数:

    tidyHTML <- function(URL) {
      require(XML)
      URL = gsub("/", "%2F", URL)
      URL <- gsub(":", "%3A", URL)
      URL <- paste("http://services.w3.org/tidy/tidy?docAddr=", URL, sep = "")
      htmlParse(URL)
    }
    

    tidyHTML我对网页抓取知之甚少,但你试图抓取的网页写得非常糟糕。例如,只有9个
    标记和27个
    标记。也许这就是问题的一部分。还要注意,这最终会忽略数据,以符合
    structure()
    调用的维度,需要具有适当长度的向量,在本例中为14 x 18。
    seq(19952012)
    显然也不理想。谢谢你的回答,我真的很感激!1+这是一个很好的解决方案。看起来很灵活。它工作得很完美!!!非常感谢。使用
    tidyHTML
    的不错的解决方案,我发现services.w3.org非常有用
    system("wget http://www.bcn.gob.ni/estadisticas/trimestrales_y_mensuales/siec/datos/4.IMAE.htm")
    system("tidy 4.IMAE.htm > new.html")
    
    library(XML)
    u <- htmlParse("new.html")
    imae <- readHTMLTable(u)
    
    imae <- readHTMLTable(u, skip.rows=c(1:5, 7, 27, 28), header=TRUE)
    imae
    # $`NULL`
    #     Año   Ene   Feb   Mar   Abr   May   Jun   Jul   Ago   Sep   Oct   Nov   Dic Promedio
    # 1  1994 101.6 107.6 100.1  95.7  94.7  92.8  92.1  96.8  98.5  97.4 101.7 121.1    100.0
    # 2  1995 113.2 105.0 113.6  98.0 100.9  95.4  99.8 101.5 108.3 107.1 107.6 133.2    107.0
    # 3  1996 123.6 116.0 109.1 107.3  94.8 101.2 100.7 115.3 110.6 112.7 117.5 137.7    112.2
    # 4  1997 133.4 115.9 117.4 118.8 120.4 108.2 107.4 111.1 120.3 117.7 119.5 142.3    119.4
    # 5  1998 131.4 120.4 127.9 118.4 130.2 116.5 122.1 129.7 127.3 127.5 112.7 156.6    126.7
    # 6  1999 146.0 139.6 146.9 134.8 140.6 131.8 130.6 128.3 128.9 131.8 142.7 172.6    139.5
    # 7  2000 157.8 142.1 147.3 138.5 137.7 135.7 128.9 131.2 141.7 143.0 156.6 191.2    146.0
    # 8  2001 163.3 143.8 154.8 141.5 147.6 134.0 135.7 143.3 138.2 138.8 145.3 187.3    147.8
    # 9  2002 152.1 144.7 143.3 142.1 143.1 131.9 136.1 145.7 146.4 147.8 157.5 185.0    148.0
    # 10 2003 159.3 151.4 149.1 142.7 139.7 139.1 145.6 147.8 154.9 158.4 157.8 195.7    153.5
    # 11 2004 172.8 157.1 166.9 153.6 161.2 150.5 155.3 153.3 156.6 155.6 167.7 213.0    163.6
    # 12 2005 183.1 170.6 173.6 158.7 160.8 158.5 158.8 168.7 165.8 165.4 178.4 218.8    171.8
    # 13 2006 187.7 177.8 185.6 161.8 166.4 163.2 164.7 175.1 175.1 185.3 189.6 231.2    180.3
    # 14 2007 200.1 184.1 196.5 180.1 169.7 171.4 181.6 180.9 173.0 182.8 202.0 236.7    188.2
    # 15 2008 205.4 194.4 193.1 205.9 171.0 174.8 181.3 190.7 183.1 182.7 182.5 244.7    192.5
    # 16 2009 195.7 191.0 190.8 177.0 168.1 172.6 179.2 185.6 178.9 181.4 191.3 241.4    187.7
    # 17 2010 195.2 193.7 205.1 185.2 179.3 190.1 191.6 190.0 193.5 197.6 210.9 266.0    199.8
    # 18 2011 213.9 207.4 217.3 198.7 196.1 198.8 191.9 210.0 203.7 207.9 217.3 274.5    211.5
    # 19 2012 233.6                                                                      233.6
    
    tidyHTML <- function(URL) {
      require(XML)
      URL = gsub("/", "%2F", URL)
      URL <- gsub(":", "%3A", URL)
      URL <- paste("http://services.w3.org/tidy/tidy?docAddr=", URL, sep = "")
      htmlParse(URL)
    }
    
    u <- tidyHTML("http://www.bcn.gob.ni/estadisticas/trimestrales_y_mensuales/siec/datos/4.IMAE.htm")
    readHTMLTable(u)