Class: Reddit::Scraper
- Inherits:
-
Object
- Object
- Reddit::Scraper
- Defined in:
- lib/reddit/scraper.rb
Instance Attribute Summary collapse
-
#author ⇒ Object
Returns the value of attribute author.
-
#comments ⇒ Object
Returns the value of attribute comments.
-
#timestamp ⇒ Object
Returns the value of attribute timestamp.
-
#title ⇒ Object
Returns the value of attribute title.
-
#upvotes ⇒ Object
Returns the value of attribute upvotes.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
Instance Attribute Details
#author ⇒ Object
Returns the value of attribute author.
2 3 4 |
# File 'lib/reddit/scraper.rb', line 2 def @author end |
#comments ⇒ Object
Returns the value of attribute comments.
2 3 4 |
# File 'lib/reddit/scraper.rb', line 2 def comments @comments end |
#timestamp ⇒ Object
Returns the value of attribute timestamp.
2 3 4 |
# File 'lib/reddit/scraper.rb', line 2 def @timestamp end |
#title ⇒ Object
Returns the value of attribute title.
2 3 4 |
# File 'lib/reddit/scraper.rb', line 2 def title @title end |
#upvotes ⇒ Object
Returns the value of attribute upvotes.
2 3 4 |
# File 'lib/reddit/scraper.rb', line 2 def upvotes @upvotes end |
#url ⇒ Object
Returns the value of attribute url.
2 3 4 |
# File 'lib/reddit/scraper.rb', line 2 def url @url end |
Class Method Details
.scrape ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/reddit/scraper.rb', line 3 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 = post.css('.author').text.strip = 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: , timestamp: , comments: comments, upvotes: upvotes, url: url, } posts << newPost end posts end |