Skip to content

Commit c8c9b22

Browse files
authored
[add] example: download comments for a post (#965)
* [add] example: download comments for a post * [fix] undo unintentionally deleted part
1 parent f659c56 commit c8c9b22

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,45 @@ Run `facebook-scraper --help` for more details on CLI usage.
7575

7676
**Note:** If you get a `UnicodeEncodeError` try adding `--encoding utf-8`.
7777

78+
### Practical example: donwload comments of a post
79+
80+
```python
81+
"""
82+
Download comments for a public Facebook post.
83+
"""
84+
85+
import facebook_scraper as fs
86+
87+
# get POST_ID from the URL of the post which can have the following structure:
88+
# https://www.facebook.com/USER/posts/POST_ID
89+
# https://www.facebook.com/groups/GROUP_ID/posts/POST_ID
90+
POST_ID = "pfbid02NsuAiBU9o1ouwBrw1vYAQ7khcVXvz8F8zMvkVat9UJ6uiwdgojgddQRLpXcVBqYbl"
91+
92+
# number of comments to download -- set this to True to download all comments
93+
MAX_COMMENTS = 100
94+
95+
# get the post (this gives a generator)
96+
gen = fs.get_posts(
97+
post_urls=[POST_ID],
98+
options={"comments": MAX_COMMENTS, "progress": True}
99+
)
100+
101+
# take 1st element of the generator which is the post we requested
102+
post = next(gen)
103+
104+
# extract the comments part
105+
comments = post['comments_full']
106+
107+
# process comments as you want...
108+
for comment in comments:
109+
110+
# e.g. ...print them
111+
print(comment)
112+
113+
# e.g. ...get the replies for them
114+
for reply in comment['replies']:
115+
print(' ', reply)
116+
```
78117

79118
## Post example
80119

0 commit comments

Comments
 (0)