facebook-scraper的get_posts方法无返回或返回内容缺失评论字段的问题求助
facebook-scraper的get_posts方法无返回或返回内容缺失评论字段的问题求助
我最近在尝试用facebook-scraper工具抓取公开Facebook页面的评论,但遇到了两个棘手的问题:要么调用get_posts时完全没有帖子返回,要么返回的内容里缺少预期的评论相关字段。
问题1:按文档示例调用无任何返回
我直接照搬文档里的示例代码抓取任天堂页面的帖子,结果运行后没有任何输出:
print("") print("") # example from docs from facebook_scraper import get_posts for post in get_posts('nintendo', pages=5): print(post['text'][:50]) print("") print("")
问题2:指定单条帖子返回内容缺失评论字段
另一个文档示例里,我指定了一个帖子ID,设置了抓取10条评论,但返回的内容里只有请求URL相关的信息,完全没有comments或comments_full这些我需要的字段:
#example from the docs POST_ID = "pfbid02NsuAiBU9o1ouwBrw1vYAQ7khcVXvz8F8zMvkVat9UJ6uiwdgojgddQRLpXcVBqYbl" # number of comments to download -- set this to True to download all comments MAX_COMMENTS = 10 # get the post (this gives a generator) gen = fs.get_posts( post_urls=[POST_ID], options={"comments": MAX_COMMENTS, "progress": True} ) # I added this to check the size gen_copy, gen = itertools.tee(gen) # Create a copy length = sum(1 for _ in gen_copy) # Count items without exhausting the original print(length) print(gen) # take 1st element of the generator which is the post we requested post = next(gen) print("post is ", post)
这段代码的输出结果如下:
1 <itertools._tee object at 0x000001608C49AB00> post is {'original_request_url': 'pfbid02NsuAiBU9o1ouwBrw1vYAQ7khcVXvz8F8zMvkVat9UJ6uiwdgojgddQRLpXcVBqYbl', 'post_url': 'https://m.facebook.com/pfbid02NsuAiBU9o1ouwBrw1vYAQ7khcVXvz8F8zMvkVat9UJ6uiwdgojgddQRLpXcVBqYbl'}
我原本期望get_posts返回的生成器里的帖子字典,应该包含comments和comments_full这些字段,方便我获取评论内容,但现在完全达不到预期。有没有人遇到过类似问题,能帮忙看看怎么解决吗?
备注:内容来源于stack exchange,提问作者user6136083
相关产品推荐
相关产品推荐

