Alessandro Cocilova
Alessandro Cocilova

Reputation: 17

Facebook search by keyword

On facebook, I used to search public posts containing specific keywords through R, using the function "searchFacebook" in the package RFacebook, but that function used an API that is discontinued in the new API 2, that is mandatory since April 30. I gave a look to the APIs and apparently there is no more a method to extract post in this way. Is that true?

Upvotes: 1

Views: 2791

Answers (2)

Blue Phoenix
Blue Phoenix

Reputation: 11

Searching public posts on FB using keywords now has been removed since they updated Graph API to v2.x.

Below is my alternative to get "public pages" in R (you can do the same for public groups - just change searchPage to searchGroup).

All the comments/posts/reactions/replies will be consolidated in 1 single dataset for analysis.

searchFB <- function(key){

cat(paste("Getting data for keyword: ",key,"\n", sep = " "))


pagelist<- select(filter(searchPages(key,x, n = 10000), 
                       category == "xxxxxxxxx"),id)

cat(paste("\nTotal of relevant pages is: ",nrow(pagelist),"\n"))
# specify which date you want to start "scrapping"
begin = "xxxx"
today = Sys.Date()

# Initiate variables

page_df <- data.frame()
#post_df <- data.frame()
comment_df <- data.frame()
replies_df <- data.frame()

#pulling data for page_df and comment_df 
for (i in 1:nrow(pagelist))
{
cat("\n")
 cat(paste("Getting posts from page number ",i," with ID: ", pagelist[i,], "\n"))
target_page <- getPage(pagelist[i,],x,n=100000, since=begin , until = today,
                       feed = TRUE, reactions = TRUE)

#Adding keyword to table 
if(!empty(target_page)){
  target_page <- cbind(key, target_page)
}
page_df <- try(rbind(page_df,target_page))

#Taken from Alex's code for checking if page has no posts
for (j in 1:nrow(target_page))
{

  if(is.null(target_page$id[j])){
  } else {
    target_post <- getPost(target_page$id[j], n=100000,  x, comments = TRUE, likes = TRUE)
    #post_df<- try(rbind(post_df,target_post$post))
    comment_df <-try(rbind(comment_df,target_post$comments))
    if (class(comment_df)=="try-error")next;
  }
}
if(class(page_df)=="try-error")next;
}

cat("\n Complete collecting. Now moving to merging phase! \n")
# Join 2 data frame to create 1 consolidated dataset for each keyword

if(!empty(page_df)){
#the 2nd part of ID
for (i in 1:nrow(page_df))
{
  x<-strsplit(page_df[i,]$id,"_")[[1]]
  y<-tolower(x)[2]
  page_df$join_id[i] <-y
}}

if(!empty(comment_df)){
#the 1st part of ID
for (i in 1:nrow(comment_df))
{
  x<-strsplit(comment_df[i,]$id,"_")[[1]]
  y<-tolower(x)[1]
  comment_df$join_id[i] <-y
}}

if(empty(page_df)) {
 final_dataset<-data.frame();
} else if (empty(comment_df)){
final_dataset<-page_df
} else {
final_dataset<-full_join(page_df,comment_df,by = c("join_id"))
}


cat("\n Writing file to .csv")
write.csv(final_dataset, file = paste(key,".csv", sep = ""),
        row.names=FALSE, qmethod='escape',
        fileEncoding = "UTF-8", na = "NA")
}
# Get data for xxx 
searchFB("xxx") #type your keyword here

Upvotes: 0

andyrandy
andyrandy

Reputation: 73994

Yes, that is true. Public Post Search is gone and there is no alternative, so you can´t search by keyword anymore.

Changelog: https://developers.facebook.com/docs/apps/changelog

Edit: It is not in the changelog anymore, seems to be too old. Either way, the answer is still correct.

Upvotes: 1

Related Questions