Skip to content

Instantly share code, notes, and snippets.

@movstox
Last active November 1, 2019 22:20
Show Gist options
  • Save movstox/7ca6115f96e2616eb2f49d741c0b0bf2 to your computer and use it in GitHub Desktop.
Save movstox/7ca6115f96e2616eb2f49d741c0b0bf2 to your computer and use it in GitHub Desktop.
scraping public fb group - first post stats
# frozen_string_literal: true
require 'selenium-webdriver'
def look_into(fb_group_url)
return unless block_given?
options = Selenium::WebDriver::Chrome::Options.new(args: ['headless', 'disable-gpu', '--window-size=750,1080'])
driver = Selenium::WebDriver.for :chrome, options: options
driver.get(fb_group_url)
sleep 3
first_post = driver.find_element(css: 'div[role=article]')
permalink_el = first_post.find_element(css: 'a[href*="permalink"')['href']
sleep 3
# load more posts
# driver.execute_script('window.scrollBy(0,1000)')
likes_count = first_post.find_element(css: '[data-testid="UFI2ReactionsCount/sentenceWithSocialContext"]').text.to_i
comments_count = first_post.find_element(css: '[data-testid="fbFeedStoryUFI/feedbackSummary"]').text.scan(/(\d+)\s(\W+)/i)&.first&.first.to_i
post_stats = {
url: fb_group_url,
likes: likes_count,
comments: comments_count
}
yield post_stats
end
page_url = 'https://www.facebook.com/groups/yosoylamilonga/'
look_into(page_url) do |first_post_stats|
puts format(%(
Looking into fb group at %{url}.
It has:
- %{likes} likes
- %{comments} comments
), first_post_stats)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment