Sha256: c6620f1a038a8af9ba5a9a4833e57db15942ac70d38962179a42048f9043c8f6

Contents?: true

Size: 832 Bytes

Versions: 3

Compression:

Stored size: 832 Bytes

Contents

class Reddit::Scraper
  attr_accessor :title, :author, :timestamp, :comments, :upvotes, :url
  def self.scrape
    doc = Nokogiri::HTML(open("https://www.reddit.com/r/ruby", 'User-Agent' => 'ruby-reddit'))
    posts = []
    postsList = doc.css('#siteTable > div.thing.link').first(10)
    postsList.each do |post|
      title = post.css('.title a.title').text.strip
      author = post.css('.author').text.strip
      timestamp = post.css('.live-timestamp').text.strip
      comments = post.attr('data-comments-count')
      upvotes = post.attr('data-score')
      url = post.css('a.comments').attr('href')
      newPost = {
        title: title,
        author: author,
        timestamp: timestamp,
        comments: comments,
        upvotes: upvotes,
        url: url,
      }
      posts << newPost
    end
    posts
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
reddit-ruby-1.0.7 lib/reddit/scraper.rb
reddit-ruby-1.0.6 lib/reddit/scraper.rb
reddit-ruby-1.0.5 lib/reddit/scraper.rb