如何在R中获得最受欢迎的Facebook帖子 [英] How to get most popular Facebook post in R

查看:226
本文介绍了如何在R中获得最受欢迎的Facebook帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码从Facebook上的页面获取帖子。即使查询在我在浏览器中输入时有效,我也会收到错误。这是我得到的错误:

I am trying to use the following code to get posts from a page on Facebook. I get an error even though the query works when I type it in a browser. This is the error I get:

WWW-Authenticate: OAuth "Facebook Platform" "invalid_request" "Unknown path components: 

非常感谢任何想法!

# go to 'https://developers.facebook.com/tools/explorer' to get your access token
access_token <- "### token ###"

require(RCurl)
require(rjson)

cafile <- system.file("CurlSSL", "cacert.pem", package = "RCurl")

options(RCurlOptions = list(verbose = TRUE, followlocation = TRUE, timeout = 100, useragent = "R"))


# set the curl options
curl <- getCurlHandle()
options(RCurlOptions = list(capath = system.file("CurlSSL", "cacert.pem",
                                             package = "RCurl"),
                        ssl.verifypeer = FALSE, verbose = TRUE, cookiejar = 'my_cookies.txt', 
                        cookiefile = 'my_cookies.txt',   followlocation = TRUE,                                                                        
                        useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3'))
curlSetOpt(.opts = list(proxy = 'proxyserver:port'), curl = curl)


# Facebook json function copied from original (Romain Francois) post
facebook <-  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 ) )
fromJSON( data )
}


### TED FACEBOOK PAGE
# http://www.facebook.com/TED
# TED's Facebook ID 29092950651 can be found on http://graph.facebook.com/TED

ted <- list()
i<-0
next.path <- "29092950651/posts"

# download all TED posts
while(length(next.path)!=0) {
  i<-i+1
  ted[[i]] <- facebook( path=next.path , access_token=access_token)
  next.path <- sub("https://graph.facebook.com/","",ted[[i]]$paging$'next')
}
ted[[i]] <- NULL

# parse just video links posted by TED
parse.count.ted <- function(x) 
  if (x$type=="link" & x$from$id=="29092950651") x$likes$count else NA
parse.link.ted <- function(x) 
  if (x$type=="link" & x$from$id=="29092950651") x$link else NA
ted.counts <- unlist(sapply(ted, parse.master, f=parse.count.ted))
ted.links <- unlist(sapply(ted, parse.master, f=parse.link.ted))

# see three most popular talks
ted.links[order(ted.counts,decreasing=TRUE)][1:3]


推荐答案

这可能是URL格式化的问题。如果未指定选项参数,则生成的URL将如下所示: / me / photos& access_token = ... 。在这里, 路径 将是 / me / photos& access_token ,根据Facebook API,它可能不是有效的URL组件。

This might be a problem of how the URL is being formatted. If the options argument is not specified, the resulting URL would look like: /me/photos&access_token=.... Here, the path would be /me/photos&access_token which probably is not a valid URL component as per Facebook API.

I认为对 facebook 函数的以下更改将解决此问题:

I think the following changes to the facebook function would fix this:

require(RCurl)
require(rjson)

facebook <-  function( path = "me", access_token = token, options){
    if( !missing(options) ){
        options <- sprintf( 
                           "?%s&", 
                           paste( 
                                 names(options), "=", unlist(options), 
                                 collapse = "&", sep = "" 
                                 ) 
                           )
    } else {
        options <- "?"
    }

    urlTemplate <- "https://graph.facebook.com/%s%saccess_token=%s"
    data <- getURL( 
                   sprintf( 
                           urlTemplate, 
                           path,
                           options,
                           access_token 
                           ) 
                    )
    fromJSON( data )
}

现在,即使缺少选项参数,生成的URL如下所示: / me / photos?access_token = ...

Now, even if the options argument is missing, the resulting URL would look like: /me/photos?access_token=....

这篇关于如何在R中获得最受欢迎的Facebook帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆