Class: NytimesTopStories::Scraper
- Inherits:
-
Object
- Object
- NytimesTopStories::Scraper
- Defined in:
- lib/nytimes_top_stories/scraper.rb
Class Method Summary collapse
- .get_date(site = "http://nytimes.com") ⇒ Object
- .get_top_stories(site = "http://nytimes.com") ⇒ Object
- .open_site(site = "http://nytimes.com") ⇒ Object
Class Method Details
.get_date(site = "http://nytimes.com") ⇒ Object
31 32 33 34 |
# File 'lib/nytimes_top_stories/scraper.rb', line 31 def self.get_date(site = "http://nytimes.com") doc = self.open_site(site) doc.css(".masthead-menu .date").text end |
.get_top_stories(site = "http://nytimes.com") ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/nytimes_top_stories/scraper.rb', line 10 def self.get_top_stories(site = "http://nytimes.com") doc = self.open_site(site) top_stories_array = [] stories = doc.css("#top-news .collection .theme-summary") stories.each do |story| story_hash = {} story_hash[:headline] = story.css(".story-heading a").text story_hash[:byline] = story.css(".byline").text.strip if !story.css("ul").empty? story_hash[:summary] = story.css("ul").text.strip else story_hash[:summary] = story.css("p.summary").text.strip end if !story.at_css(".story-heading a").nil? story_hash[:url] = story.css(".story-heading a").attr("href").value end top_stories_array << story_hash end top_stories_array end |
.open_site(site = "http://nytimes.com") ⇒ Object
5 6 7 8 |
# File 'lib/nytimes_top_stories/scraper.rb', line 5 def self.open_site(site = "http://nytimes.com") html = open(site) doc = Nokogiri::HTML(html) end |