An asynchronous Python tool using Playwright to extract engagement metrics (Reactions, Comments, Shares), post titles, publication dates, and deep comment threads from Facebook posts, photos, and Reels.
Rather than relying purely on DOM elements, this toolkit intercepts background GraphQL network requests to retrieve exact, un-abbreviated counts and raw comment payloads directly from API responses.
- Network Interception: Captures raw GraphQL payloads to retrieve precise metrics (e.g., 1,542 instead of 1.5K) and full comment text streams in real time.
- Deep Comment Thread Scraping (
comment_scraper.py): Automatically expands collapsed sub-replies, navigates nested comment hierarchies, and links child replies to parent comment IDs. - Adaptive Early-Exit Controller: Monitors stream activity to automatically terminate comment loops after consecutive stagnant checks.
- Session Helper: Includes
save_session.pyto log in once via an interactive browser window and export your session state toauth.json. - Concurrent Processing: Uses Python
asyncioandasyncio.Semaphoreto process multiple engagement URLs in parallel. - Resource Optimization: Blocks heavy static assets (images, video streams, web fonts) to maximize network bandwidth for GraphQL stream processing.
- Dual Export: Exports high-level post engagement to CSV and structured JSON, and deep comment hierarchies to a dedicated JSON file.
- Python 3.8 or higher
- Playwright for Python
-
Clone the repository:
git clone [https://github.com/pring-nt/fb-graphql-interceptor.git](https://github.com/pring-nt/fb-graphql-interceptor.git) cd fb-graphql-interceptor -
Install dependencies:
pip install playwright
-
Install Chromium browser binary:
playwright install chromium
An authenticated session (auth.json) is required for deep comment scraping to prevent Facebook from displaying login walls or blocking comment expansion.
python save_session.py
Log in to Facebook in the browser window that opens, then return to your terminal and press Enter to save the session state to auth.json.
Create a text file named links.txt containing the Facebook URLs you wish to scrape, one per line. Lines starting with # are ignored.
[https://www.facebook.com/watch/?v=123456789](https://www.facebook.com/watch/?v=123456789)
[https://www.facebook.com/share/v/AbCdEfGhIj/](https://www.facebook.com/share/v/AbCdEfGhIj/)
# Optional comment line
[https://www.facebook.com/photo/?fbid=1722547075703056](https://www.facebook.com/photo/?fbid=1722547075703056)
Extract high-level post titles, dates, reactions, total comments, and share counts:
python scraper.py
Extract full text comment threads, author names, timestamps, and sub-reply structures:
python comment_scraper.py
MAX_CONCURRENT = 3 # Number of browser tabs running in parallel DELAY_SEC = 2.0 # Pause duration after page navigation
MAX_CONCURRENT = 1 # Keep low to avoid rate limits during deep scrolling TARGET_COMMENTS_PER_POST = 500# Upper comment capture target per post MAX_SCROLL_ATTEMPTS = 60 # Maximum scroll ticks allowed per post MAX_STAGNANT_TICKS = 3 # Exit loop after N consecutive ticks with 0 new comments
url: Source URLdate: Post publication date (YYYY-MM-DD)title: Post title or caption textreactions: Total reaction countcomments: Total comment countshares: Total share counttotal_engagement: Aggregate engagement sumstatus/error: Execution status and error logs
[
{
"url": "[https://www.facebook.com/photo/?fbid=1722547075703056](https://www.facebook.com/photo/?fbid=1722547075703056)",
"scraped_at": "2026-08-27T19:28:14.000000",
"total_comments_captured": 8,
"status": "ok",
"error": "",
"comments": [
{
"comment_id": "10161829304928102",
"author": "Jane Doe",
"text": "Great post!",
"date": "2026-08-25 14:22:10",
"is_reply": false,
"parent_id": null
},
{
"comment_id": "10161829311204911",
"author": "John Smith",
"text": "Agreed with Jane!",
"date": "2026-08-25 14:30:05",
"is_reply": true,
"parent_id": "10161829304928102"
}
]
}
]