Skip to content

Commit

Permalink
Merge branch 'master' of github.com:kevinzg/facebook-scraper
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-ninja committed Aug 16, 2022
2 parents f1c40d9 + 8106792 commit 4ffeb1e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
12 changes: 12 additions & 0 deletions facebook_scraper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,18 @@ def handle_pagination_url(url):
output_file.close()


def get_groups_by_search(
word: str,
**kwargs,
):
"""Searches Facebook groups and yields ids for each result
on the first page"""
_scraper.requests_kwargs['timeout'] = kwargs.pop('timeout', DEFAULT_REQUESTS_TIMEOUT)
cookies = kwargs.pop('cookies', None)
set_cookies(cookies)
return _scraper.get_groups_by_search(word, **kwargs)


def enable_logging(level=logging.DEBUG):
handler = logging.StreamHandler()
handler.setLevel(level)
Expand Down
20 changes: 20 additions & 0 deletions facebook_scraper/facebook_scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1098,3 +1098,23 @@ def _generic_get_posts(
if remove_source:
post.pop('source', None)
yield post

def get_groups_by_search(self, word: str, **kwargs):
group_search_url = utils.urljoin(FB_MOBILE_BASE_URL, f"search/groups/?q={word}")
r = self.get(group_search_url)
for group_element in r.html.find('div[role="button"]'):
button_id = group_element.attrs["id"]
group_id = self.find_group_id(button_id, r.text)
try:
yield self.get_group_info(group_id)
except AttributeError:
continue


@staticmethod
def find_group_id(button_id, raw_html):
"""Each group button has an id, which appears later in the script
tag followed by the group id."""
s = raw_html[raw_html.rfind(button_id) :]
group_id = s[s.find("result_id:") :].split(",")[0].split(":")[1]
return int(group_id)

0 comments on commit 4ffeb1e

Please sign in to comment.