2

我正在尝试从各个页面提取 Facebook 提要数据以比较情绪,并且在将 JSON 原始文本转换为 R 中的列表对象时遇到了麻烦。

require(RCurl)
require(rjson)
access_token <- "XXXXXXXXXXXXXXXX"

FacebookScrape <-  function( path = "me", access_token, options){
  if( !missing(options) ){
    options <- sprintf( "?%s", paste( names(options), "=", unlist(options), collapse = "&", sep = "" ) )
  } else {
    options <- ""
  }
  data <- getURL( sprintf( "https://graph.facebook.com/%s%s&access_token=%s", path, options, access_token ),
                  ssl.verifypeer = FALSE)
  fromJSON(data, unexpected.escape = "skip")
}

cb.path <- "24329337724/feed?limit=300&offset=0&__after_id=354707562896&"
cb.feed <- FacebookScrape(path = cb.path, access_token = access_token)

此代码返回以下错误消息:

Error in fromJSON(data, unexpected.escape = "skip") : 
  unexpected character: c

我对 JSON 不是很熟悉,但我知道错误发生在 fromJSON 函数中(上面代码中的第 13 行)。这个函数调用 C,所以使用 debug() 并不能告诉我太多。如果 JSON 文本格式正确,我也不确定一个简单的字符“c”如何导致错误。它不像“c”是一个转义字符或任何东西。我还使用unexpected.escape = "skip"fromJSON 中的选项来考虑转义字符。

我已经确定解析这篇文章时会发生错误(如果我设置limit=261了没有错误cb.path,但如果我有limit=262)。有没有人遇到过类似的问题?任何帮助将不胜感激。

会话信息:

R version 2.15.3 (2013-03-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] streamR_0.1        wordcloud_2.2      RColorBrewer_1.0-5 Rcpp_0.10.2        stringr_0.6.2     
 [6] plyr_1.8           tm_0.5-8.3         twitteR_1.1.6      rjson_0.2.12       ROAuth_0.9.3      
[11] digest_0.6.2       ggplot2_0.9.3.1    XML_3.95-0.1       RCurl_1.95-4.1     bitops_1.0-5      

loaded via a namespace (and not attached):
 [1] colorspace_1.2-1 dichromat_2.0-0  grid_2.15.3      gtable_0.1.2     labeling_0.1     MASS_7.3-23     
 [7] munsell_0.4      proto_0.3-10     reshape2_1.2.2   scales_0.2.3     slam_0.1-27      tools_2.15.3    
4

2 回答 2

1

我有同样的问题......基于来自 Rfacebook 的 callAPI:https ://github.com/pablobarbera/Rfacebook/blob/master/Rfacebook/R/utils.R 使用:fromJSON(rawToChar(data)

facebook <- function(url, token){
  if (class(token)=="config"){
    url.data <- GET(url, config=token)
  }
  if (class(token)=="Token2.0"){
    url.data <- GET(url, config(token=token))
  } 
  if (class(token)=="character"){
    url <- paste0(url, "&access_token=", token)
    url <- gsub(" ", "%20", url)
    url.data <- GET(url)
  }
  if (class(token)!="character" & class(token)!="config" & class(token)!="Token2.0"){
    stop("Error in access token. See help for details.")
  }
  content <- fromJSON(rawToChar(url.data$content)) # It's working very well
  if (length(content$error)>0){
    stop(content$error$message)
  } 
  return(content)
}

调用脸书功能:

next.path <- "https://graph.facebook.com/29092950651/posts"
facebook( url=next.path , token)

您的 access_token 将激活超过 2 小时。我使用基于http://blog.revolutionanalytics.com/2013/11/how-to-analyze-you-facebook-friends-network-with-r.html的 fb_oauth

最好的问候罗伯特

于 2014-04-28T09:19:28.630 回答
0

我检查了你的 JSON

原因在这里

“消息”:“真实\”,

这导致 R 中的 json 被解析并变为 \" 并且缺少的引号消失了。

下一行 can_comment 触发错误,它以 C 开头

于 2014-11-27T13:24:17.287 回答