【问题标题】:Why is my Rfacebook loop script not working when there is a post with zero comments?当有零评论的帖子时,为什么我的 Rfacebook 循环脚本不起作用?
【发布时间】:2017-06-06 07:39:23
【问题描述】:

我已将我的问题编辑为更相关

从我开始学习 R 到现在才不到一个月,我正试图用它来摆脱我们用于报告的与 Facebook 相关的繁琐工作(提取 cmets)。

使用 Rfacebook 包,我制作了这个脚本,它在其中提取 (1) 给定时间段内页面的帖子,以及 (2) 评论那些帖子。它对我正在为其进行报告的页面运行良好,但是当我在其他页面上尝试它时,它的帖子的 cmets 为零,它报告了一个错误。

这是脚本: 加载库

library(Rfacebook)
library(lubridate)
library(tibble)

设置时间段。随意更改时间。

current_date <-Sys.Date()
past30days<-current_date-30

分配页面。将此编辑到您正在监视的页面*

brand<-'bpi'

验证 Facebook。使用你自己的

app_id <- "xxxxxxxxxxxxxxxx"
app_secret <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
token <- fbOAuth(app_id,app_secret,extended_permissions = FALSE,legacy_permissions = FALSE)

从页面中提取所有帖子

listofposts <- getPage(brand, token, n = 5000, since = past30days, until = current_date, feed = FALSE, reactions = FALSE, verbose=TRUE)
write.csv(listofposts,file = paste0('AsOf',current_date,brand,'Posts','.csv'))

转换为数据框

df<-as_tibble(listofposts)

转换为向量

postidvector<-df[["id"]] 

获取该期间的帖子数

n<-length(postidvector)

通过循环产生所有的cmets

reactions<-vector("list",n)
for(i in 1:n){
  reactions[[i]]<-assign(paste(brand,'Comments', i, sep = ""), (getPost((postidvector[i]),token,comments=T,likes=F,n.likes=5000,n.comments=10000))) 
}

将每个帖子的所有 cmets 提取到 CSV

for(j in 1:n){
  write.csv(reactions[[j]],file = paste0('AsOf',current_date,brand,'Comments' ,j, '.csv'))
}

当我在 包含零 cmets 的帖子的页面:

Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
  arguments imply differing number of rows: 1, 0

我在流量很大的页面上尝试过,效果也很好。一个帖子有 10,000 cmets,它提取得很好。 :(

提前致谢! :D

【问题讨论】:

    标签: r facebook-graph-api rfacebook


    【解决方案1】:

    页面可以受年龄或位置限制。您不能对这些使用应用访问令牌,因为它不包含用户会话,因此 Facebook 不知道您是否可以查看页面内容。您必须使用用户令牌或页面令牌。

    【讨论】:

    • App Access Token 独立于会话,被认为是永久令牌。所以它可能对你有用。
    • @Aditi 如果页面受到限制,则不能使用应用令牌;)
    • 对不起,我忘了提到“公共页面”:)
    • 所有页面都是公开的,但有些受到年龄或位置的限制。老实说,我仍然不确定你想说什么。
    • 所有页面都是公开的 :) 我想我已经看到了哪里出了问题。有时,当帖子没有 cmets 时,循环会中断。
    猜你喜欢
    • 2021-05-24
    • 1970-01-01
    • 2015-01-13
    • 2019-11-14
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多