How to get the total number of posts in a Facebook page with Graph API?
Asked Answered
P

5

8

I am using Facebook Graph API to crawl information from public page. The current problem is how to get the total number of posts on a Facebook page. If we go to /{page-id}/posts, it will only return 25 posts posted on this page, and no summary information mentioned the page's fields. I have check the previous answer, it seems that the only way is to count the number of items of each link in next and get total number. But it is very inefficient. Is there any method that can directly get the total post on a page other than FQL?

Preponderant answered 25/10, 2014 at 8:34 Comment(0)
H
1

Facebook doesn't actively count the number of posts a page has made; depending on the page it could be an astronomical number.

You would have to get all the posts and count through them yourself. I would use something like this.

{PAGE}/posts?fields=id&limit=250

It will return the smallest possible data set you need. You can't go above 250, you could with FQL but not with v2.1. Also you don't want FEED because that is an aggregation of the Page's Posts and the Posts made to the page by other users. This will return an object like this.

{
 "data" : [
  PostData
  ... ],
 "paging" : {
  ...
  "next" : CursorURL
 }
}

This is the cursor URL so you can step down the groups of 250 posts in reverse chronological order.

You can shortcut the counting by incrementing 250 every time you get a new cursor that also returns more posts. You will only have to count and add the results in the set with the 2nd to last cursor since the last cursor will return an empty data array.

Halla answered 27/10, 2014 at 16:49 Comment(2)
Max limit is 100 for posts. Dont know where you got the 250 from.Verlie
@Verlie It was 250, but it is now 100, and if you put more than 100 it will actually give you an error instead of just limiting the results to 100.Woundwort
G
5

You can only retrieve a summary/count for published posts on a page. Something like this will work when querying the Page directly:

{PAGE}?fields=published_posts.limit(1).summary(total_count).since(1)

The response will look something like:

{
    published_posts: {
        summary: {
            total_count: 12345
        },
        ...
    }
}

or if you want to query the published_posts directly, use:

{PAGE}/published_posts?limit=1&summary=total_count&since=1

The response will look something like:

{
    summary: {
        total_count: 12345
    },
    ...
}

Setting limit to 1 ensures that your query is not unnecessarily large. If you're just needing the total post count, then set limit to 1.

The since parameter is a unix timestamp (milliseconds since January 1, 1970) and is required to retrieve the total post count. Set this to 1 as setting it to 0 will throw an error.

More details: https://developers.facebook.com/docs/graph-api/reference/page/published_posts/

Gauhati answered 14/2, 2019 at 21:0 Comment(0)
M
2
     function p_post() {
                FB.api("/209652442394600/posts?fields=admin_creator,name&limit=250", function (response) {
                    var t = response.data.length;
                    document.getElementById('tposts').innerHTML = t;
            });
            }

it works till 250 and i used it last week

Melodeemelodeon answered 16/4, 2015 at 11:17 Comment(0)
H
1

Facebook doesn't actively count the number of posts a page has made; depending on the page it could be an astronomical number.

You would have to get all the posts and count through them yourself. I would use something like this.

{PAGE}/posts?fields=id&limit=250

It will return the smallest possible data set you need. You can't go above 250, you could with FQL but not with v2.1. Also you don't want FEED because that is an aggregation of the Page's Posts and the Posts made to the page by other users. This will return an object like this.

{
 "data" : [
  PostData
  ... ],
 "paging" : {
  ...
  "next" : CursorURL
 }
}

This is the cursor URL so you can step down the groups of 250 posts in reverse chronological order.

You can shortcut the counting by incrementing 250 every time you get a new cursor that also returns more posts. You will only have to count and add the results in the set with the 2nd to last cursor since the last cursor will return an empty data array.

Halla answered 27/10, 2014 at 16:49 Comment(2)
Max limit is 100 for posts. Dont know where you got the 250 from.Verlie
@Verlie It was 250, but it is now 100, and if you put more than 100 it will actually give you an error instead of just limiting the results to 100.Woundwort
L
0

used this in console:

var posts = document.querySelectorAll('div.x1yztbdb.x1n2onr6.xh8yej3.x1ja2u2z div.x1iorvi4.x1pi30zi.x1l90r2v.x1swvt13');
var postsCount = document.querySelectorAll('div.x1yztbdb.x1n2onr6.xh8yej3.x1ja2u2z');
var keyword = "ข้าวหน้าบะเต็ง"; // Specify the keyword you want to search
var totalPosts = postsCount.length;
var keywordCount = 0;

var x1htElements = document.querySelectorAll('div.xdj266r.x11i5rnm.xat24cr.x1mh8g0r.x1vvkbs.x126k92a div[role="button"]');

x1htElements.forEach(function(element) {
    element.click();
});

posts.forEach(function(post) {
    var postText = post.textContent.trim();
    var count = countKeywordOccurrences(postText, keyword);
    if (count > 0) {
        keywordCount += 1;
        console.log("Post text:", postText);
        console.log("Keyword count:", count);
    }
});

console.log("Total posts:", totalPosts);
console.log("Total keyword count:", keywordCount);

var keywordPercentage = (keywordCount / totalPosts) * 100;
console.log("Keyword percentage:", keywordPercentage.toFixed(2) + "%");

if (keywordPercentage > 70) {
    console.log("The keyword appears in more than 70% of the total posts. 🎉");
} else {
    console.log("The keyword does not appear in more than 70% of the total posts. 😔");
}

function countKeywordOccurrences(text, keyword) {
    var regex = new RegExp(keyword, "gi");
    var matches = text.match(regex);
    return matches ? matches.length : 0;
}`enter code here`
Landlordism answered 14/6, 2023 at 19:23 Comment(0)
C
-2

You can try adding /page/posts?limit=1000 but I'm not sure that will work. You can also try using /page/feed to see if it obtains your desired results. If neither of these things work, then it is most likely that you can not retrieve the total number of posts. Also, FQL will be deprecated next year.

Cosmopolitan answered 25/10, 2014 at 17:14 Comment(1)
That's not possible. Max limit for posts is 100.Verlie

© 2022 - 2024 — McMap. All rights reserved.