24
May
2023

Facebook Scraper

https://github.com/kevinzg/facebook-scraper/blob/master/README.md

import json

from facebook_scraper import get_posts

def export_posts_as_json(posts, file_path):

    with open(file_path, ‘w’, encoding=’utf-8′) as file:

        json.dump(posts, file, ensure_ascii=False, indent=4)

try:

    posts = []

    for post in get_posts(

        ‘hk01wemedia’,

        pages=3,

        extra_info=[‘likes’, ‘comments’, ‘shares’],

        cookies=”facebook_cookies.txt”,

    ):

        post_data = {

            ‘post_id’: post[‘post_id’],

            ‘image’: post[‘image’],

            ‘video’: post[‘video’],

            ‘post_url’: post[‘post_url’],

            ‘username’: post[‘username’],

            ‘reactions’: post[‘reactions’],

            ‘text’: post[‘text’],

            ‘likes’: post[‘likes’],

            ‘comments’: post[‘comments’],

            ‘shares’: post[‘shares’],

            ‘shared_post_url’: post[‘shared_post_url’],

            ‘shared_text’: post[‘shared_text’],

        }

        posts.append(post_data)

        #print(post[‘text’][:50])

    # Export posts as JSON

    export_posts_as_json(posts, ‘posts.json’)

except Exception as e:

    print(f”An exception occurred: {e}”)

Share

You may also like...