Facebook-Scraper(无API)登录失败:如何配置登录流程?
解决Facebook-Scraper的LoginRequired错误
问题背景
在Google Colab环境中运行Facebook-Scraper抓取指定帖子评论时,触发LoginRequired错误,提示需要登录权限才能访问目标页面。
原执行代码:
import facebook_scraper as fs # get POST_ID from the URL of the post which can have the following structure: # https://www.facebook.com/USER/posts/POST_ID # https://www.facebook.com/groups/GROUP_ID/posts/POST_ID POST_ID = "pfbid02NsuAiBU9o1ouwBrw1vYAQ7khcVXvz8F8zMvkVat9UJ6uiwdgojgddQRLpXcVBqYbl" # number of comments to download -- set this to True to download all comments MAX_COMMENTS = 100 # get the post (this gives a generator) gen = fs.get_posts( post_urls=[POST_ID], options={"comments": MAX_COMMENTS, "progress": True} ) # take 1st element of the generator which is the post we requested post = next(gen) # extract the comments part comments = post['comments_full'] # process comments as you want... for comment in comments: # e.g. ...print them print(comment) # e.g. ...get the replies for them for reply in comment['replies']: print(' ', reply)
错误信息:
LoginRequired Traceback (most recent call last) <ipython-input-5-19c42c721928> in <cell line: 18>() 16 17 # take 1st element of the generator which is the post we requested ---> 18 post = next(gen) 19 20 # extract the comments part 1 frames /usr/local/lib/python3.10/dist-packages/facebook_scraper/facebook_scraper.py in get(self, url, **kwargs) 940 or response.url.startswith(utils.urljoin(FB_W3_BASE_URL, "login")) 941 ): ---> 942 raise exceptions.LoginRequired( 943 "A login (cookies) is required to see this page" 944 ) LoginRequired: A login (cookies) is required to see this page
解决方案
根据Facebook-Scraper的参数说明,有两种方式配置登录权限:
方法一:使用浏览器导出的Cookies(推荐)
这种方式避免直接在Colab输入账号密码,降低触发Facebook风控的概率:
- 本地浏览器登录Facebook,使用浏览器插件导出Netscape格式的
cookies.txt文件 - 将
cookies.txt上传到Colab的文件目录中(左侧文件面板点击上传按钮) - 修改代码,在
get_posts中添加cookies参数指向该文件路径:
修改后的代码:
import facebook_scraper as fs POST_ID = "pfbid02NsuAiBU9o1ouwBrw1vYAQ7khcVXvz8F8zMvkVat9UJ6uiwdgojgddQRLpXcVBqYbl" MAX_COMMENTS = 100 # 添加cookies参数,指向上传的cookies.txt文件路径 gen = fs.get_posts( post_urls=[POST_ID], cookies="/content/cookies.txt", # 注意路径要和实际上传位置一致 options={"comments": MAX_COMMENTS, "progress": True} ) post = next(gen) comments = post['comments_full'] for comment in comments: print(comment) for reply in comment['replies']: print(' ', reply)
方法二:使用账号密码登录(不推荐)
直接传入账号密码元组,但Facebook可能会检测到异常登录(如异地IP),需要验证码或两步验证,Colab环境下处理较繁琐:
修改后的代码:
import facebook_scraper as fs POST_ID = "pfbid02NsuAiBU9o1ouwBrw1vYAQ7khcVXvz8F8zMvkVat9UJ6uiwdgojgddQRLpXcVBqYbl" MAX_COMMENTS = 100 # 添加credentials参数,传入(用户名, 密码)元组 gen = fs.get_posts( post_urls=[POST_ID], credentials=("your_facebook_username", "your_facebook_password"), options={"comments": MAX_COMMENTS, "progress": True} ) post = next(gen) comments = post['comments_full'] for comment in comments: print(comment) for reply in comment['replies']: print(' ', reply)
内容的提问来源于stack exchange,提问作者zero
相关产品推荐
相关产品推荐

