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?
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.
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/
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
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.
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`
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.
© 2022 - 2024 — McMap. All rights reserved.