如何解决“文件(con,“ r”)中的HTTP状态为'409冲突'错误”

时间:2019-05-17 01:44:02

标签: r api

当我从UNComtrade下载数据并尝试通过API访问数据,然后将结果另存为csv文件在本地笔记本电脑上时,我收到一条错误消息:

cannot open URL 'https://comtrade.un.org/api/get?max=50000&type=C&freq=M&px=HS&ps=201201,201202,201203&r=344&p=842&rg=2&cc=25,26,27,28,29,30,31,32,33,34,35,36,37,38&fmt=json': HTTP status was '409 Conflict' Error in file(con, "r") : 
  cannot open the connection to 'http://comtrade.un.org/api/get?max=50000&type=C&freq=M&px=HS&ps=201201,201202,201203&r=344&p=842&rg=2&cc=25,26,27,28,29,30,31,32,33,34,35,36,37,38&fmt=json'" 

有什么想法吗?我没有遇到其他所有查询,只是一个非常具体的查询。当我在线https://comtrade.un.org/data/搜索数据时,我可以看到数据-因此数据为非空

这就是我获取数据的方式

library(dplyr)
get.Comtrade <- function(url="http://comtrade.un.org/api/get?"
                         ,maxrec=50000
                         ,type="C"
                         ,freq="A"
                         ,px="HS"
                         ,ps="now"
                         ,r
                         ,p
                         ,rg="all"
                         ,cc="TOTAL"
                         ,fmt="json"
)
{
  string<- paste(url
                 ,"max=",maxrec,"&" #maximum no. of records returned
                 ,"type=",type,"&" #type of trade (c=commodities)
                 ,"freq=",freq,"&" #frequency
                 ,"px=",px,"&" #classification
                 ,"ps=",ps,"&" #time period
                 ,"r=",r,"&" #reporting area
                 ,"p=",p,"&" #partner country
                 ,"rg=",rg,"&" #trade flow
                 ,"cc=",cc,"&" #classification code
                 ,"fmt=",fmt        #Format
                 ,sep = ""
  )

  if(fmt == "csv") {
    raw.data<- read.csv(string,header=TRUE)
    return(list(validation=NULL, data=raw.data))
  } else {
    if(fmt == "json" ) {
      #import the raw data
      raw.data<- fromJSON(file=string)
      #choose the particular dataset section of the raw data 
      data<- raw.data$dataset
      #I mean, datasets are also lists
      #that that the list (i.e. the datasets into a vector)
      validation<- unlist(raw.data$validation, recursive=TRUE)
      ndata<- NULL
      #checking if error doesn't happen
      if(length(data)> 0) {
        #show the names
        var.names<- names(data[[1]])
        #paste(data[[1]])
        #bind the data again and transpose
        data<- as.data.frame(t(sapply(data,rbind)))
        ndata<- NULL
        for(i in 1:ncol(data)){
          #doesn't it return a list of true/ false?? - how can i use it as indices?
          data[sapply(data[,i],is.null),i]<- NA
          #bind it col by col for ndata
          ndata<- cbind(ndata, unlist(data[,i]))
        }
        #as.data.frame
        ndata<- as.data.frame(ndata)
        #finally get the colnames
        colnames(ndata)<- var.names
        ndata <- ndata %>% select("cmdCode", "cmdDescE", "period", "periodDesc", "rgDesc", "rtTitle", "ptTitle", "TradeValue")
        colnames(ndata) <- c("Classification_Code", "Classification_Code_Description", "Period", "Period_Date", "X/I", "Reporter", "Partner", "Trade_Value")
      }
      return(list(validation=validation,data =ndata))
    }
  }
}

0 个答案:

没有答案