楼主: foreseer201
3694 3

[问答] xmlTreeParse方法针对URL时报错及临时解决办法 [推广有奖]

  • 0关注
  • 0粉丝

本科生

87%

还不是VIP/贵宾

-

威望
0
论坛币
1666 个
通用积分
5.4017
学术水平
1 点
热心指数
1 点
信用等级
1 点
经验
1025 点
帖子
70
精华
0
在线时间
137 小时
注册时间
2007-4-15
最后登录
2022-3-30

楼主
foreseer201 发表于 2012-1-9 21:05:09 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
http://gs-servers.com/R%E8%AF%AD ... %84GDP-6465179.html
这个网上的例子提到WDI包,给出了从世界银行的api接口获取数据的方法,
DF <- WDI(country = c("CN","RU","BR","ZA","IN"), indicator = "NY.GDP.MKTP.KD.ZG",
          start = 1990, end = 2010, extra = FALSE)
但是调用的时候报错,如下信息:
"No such file or directoryfailed to load external entity”
跟进到源代码中看了一下,报错的地方就是dat <- xmlTreeParse(daturl, useInternal = TRUE)这句,daturl是个(有效的)网址。

但是如果像下面这样,先download.file下来,再从本地的xml中读取却没问题。最后没办法,自己改了个下面的function getWDI来拿数据,跟原来方法的区别就是下面两句。
# download from URL
download.file(URL, localFile, mode = "wb")
# read from the downloaded file
dat <- xmlTreeParse(localFile, useInternal = TRUE)

不知道为什么直接从url解析不行,跟本地机器的什么环境配置有关么?大家有没有遇到过这个问题,指教下。[s:13]
  1. DF <- getWDI(country = c("CN","RU","BR","ZA","IN"), indicator = "NY.GDP.MKTP.KD.ZG",
  2.           start = 1990, end = 2010, extra = FALSE)

  3. ################################################################################
  4. # download data from world bank api and then read from the downloaded xml files
  5. ################################################################################
  6. getWDI <- function(country = "all", indicator = "NY.GNS.ICTR.GN.ZS", start = 2006,
  7.                    end = 2010, extra = FALSE) {
  8.   ## Clean user error
  9.   indicator <- sub("^[ \t]+|[ fire\t]+$", "", indicator)
  10.   country <- sub("^[ \t]+|[ \t]+$", "", country)

  11.   ## Download data in XML format for each country and each indicator
  12.   dat.full <- NULL
  13.   dat.indicator <- NULL
  14.   for (a in indicator) {
  15.     for (b in country) {
  16.       localFile <- paste(b, a, "xml", sep = ".")
  17.       daturl <- paste("http://api.worldbank.org/countries/", b,
  18.                       "/indicators/", a,
  19.                       "?date=",start,":",end,
  20.                       "&per_page=25000",
  21.                       "&format=XML",
  22.                       sep = "")
  23.       # download from URL
  24.       download.file(daturl , localFile, mode = "wb")
  25.       # read from the downloaded file
  26.       dat <- xmlTreeParse(localFile, useInternal = TRUE)
  27.       iso2c <- unlist(lapply(getNodeSet(dat, "//wb:country"), xmlAttrs))
  28.       dat <- xmlToDataFrame(dat)[,2:4]
  29.       dat$iso2c <- iso2c

  30.       ## Clean to ensure smooth merging
  31.       colnames(dat) <- c("country","year", a, "iso2c")
  32.       dat$year <- as.numeric(as.character(dat$year))
  33.       dat$iso2c <- gsub(" ", "", as.character(dat$iso2c))
  34.       dat$country <- gsub(" ", "", as.character(dat$country))
  35.       dat.indicator <- rbind(dat.indicator, dat)
  36.       daturl <- NULL
  37.       localFile <- NULL
  38.     }

  39.     ## Merge
  40.     if(is.null(dat.full)){
  41.       dat.full <- dat.indicator
  42.     }else{
  43.       dat.full <- merge(dat.full, dat.indicator, all=TRUE)
  44.     }
  45.     dat.indicator <- NULL
  46.   }

  47.   ## Clean data
  48.   dat.full <- dat.full[order(dat.full$iso2c, dat.full$year), c("country","iso2c", "year", indicator)]
  49.   for (i in 3:ncol(dat.full)){dat.full[,i] <- as.numeric(as.character(dat.full[,i]))}

  50.   ## Extras
  51.   if(extra==TRUE){
  52.     dat.full <- merge(dat.full, extra2010, all.x=TRUE)
  53.   }
  54.   return(dat.full)
  55. }
复制代码
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Tree 解决办法 url EPA ARS 接口 网上

沙发
DM小菜鸟 发表于 2014-12-15 20:16:53
这个命令和 系统(win\linux\mac) 有关,还和 http\https 协议有关,这个命令在 win8\64 位下没法下载,或者下载之后是破损的文件。
  
有一个包叫 downloader ,使用这个包的函数 download 可以正确下载文件,而且,download 和 download.file 参数是一致。

给个例子你——
install.packages("downloader")
library(downloader)

furl <- "https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Fss06hid.csv"
download(url=furl,destfile="./asc.csv")





已有 1 人评分论坛币 热心指数 收起 理由
admin_kefu + 25 + 1 热心帮助其他会员

总评分: 论坛币 + 25  热心指数 + 1   查看全部评分

藤椅
dengfoby 发表于 2015-3-14 18:53:52
LZ,我现在也遇到这个问题了,唉。
win8 x64位
难道必须要下载XML才能解析吗?

板凳
MLearning 发表于 2015-12-11 23:34:27
我也遇到了同样的问题,猜测可能是由于https导致的吧,没办法,只有先下载再加载了。

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-3 10:54