如何使用facebook_scraper抓取我拥有凭据的封闭facebook群组

2024-05-31 23:27:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用facebook_scraper来尝试抓取我所属的封闭群

登录我的凭据后,它将与不可搜索的私人组一起工作,而与可搜索的私人组一起工作(所谓可搜索,我的意思是,如果您不是该组的成员,并且在搜索栏中键入该组的名称,它将显示一些信息,但不会显示帖子或讨论,而不可搜索的组根本不会显示,如果您键入其特定url,则会重定向到登录)

我认为这是因为当一个不可搜索的组的url被输入时,你会立即被重定向到一个登录页面,这样登录就可以工作了,而可搜索的组会显示一些信息,而scraper只是在抓取这些信息

在facebook scraper的GitHub回购之后,我想我已经找到了问题所在:

facebook_scraper.page_iterators.generic_iter_pages(url, GroupPageParser, FacbookScraper.get)

这一问题在以下方面变得更加明显:

facebook_scraper.page_iterators.GroupPageParaser(url).get_html().find('article')

如果组可搜索,则返回空列表;如果组不可搜索,则返回填充列表

我与我所在的两个实际组进行的暂停调试的完整示例:

>>> from facebook_scraper import FacebookScraper, page_iterators

>>> scraper = FacebookScraper()
>>> parser = page_iterators.GroupPageParser

>>> credentials = (myemail@email.com, mypassword)
>>> scraper.login(credentials[0], credentials[1]) #It's not a login problem except for with the searchable groups

>>> searchable_url = 'https://m.facebook.com/groups/1401745746503709'
>>> unsearchable_url = 'https://m.facebook.com/groups/618892088578525'

>>> searchable_get = scraper.get(seachable_url)
>>> unsearchable_get = scraper.get(unsearchable_url)

>>> searchable_html = parser(searchable_get).get_html()
>>> unsearchable_html = parser(unsearchable_get).get_html()

>>> searchable_get.html
<HTML url='https://m/facebook.com/groups/1401745746503709' #This stays the same
>>> unsearchable_get.html
<HTML url='https://m.facebook.com/groups/618892088578525?_rdr' #This url is changed to a redirection to login

>>> len(searchable_html.find('article'))
0
>>> len(unsearchable_html.find('article'))
21

我正试图用python来解决这个问题,我对html一点也不熟悉,所以这很棘手。任何帮助都将不胜感激。 谢谢


Tags: httpscom信息urlgetfacebookhtmlarticle