Class: Tmxnews::Scraper

Inherits:
Object
  • Object
show all
Defined in:
lib/tmxnews/scraper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#textObject

Returns the value of attribute text.



2
3
4
# File 'lib/tmxnews/scraper.rb', line 2

def text
  @text
end

#titleObject

Returns the value of attribute title.



2
3
4
# File 'lib/tmxnews/scraper.rb', line 2

def title
  @title
end

#urlObject

Returns the value of attribute url.



2
3
4
# File 'lib/tmxnews/scraper.rb', line 2

def url
  @url
end

Class Method Details

.scrape_nytObject



4
5
6
7
8
9
10
11
# File 'lib/tmxnews/scraper.rb', line 4

def self.scrape_nyt
    doc = Nokogiri::HTML(open("https://www.nytimes.com"))
    article = self.new
    article.title = doc.search("h2.story-heading").first.text
    article.text = doc.search("article").css("ul li").text
    article.url = doc.search("h2.story-heading").first.css("a").attr("href").value
    article
end

.scrape_wsjObject



13
14
15
16
17
18
19
20
# File 'lib/tmxnews/scraper.rb', line 13

def self.scrape_wsj
    doc = Nokogiri::HTML(open("https://www.wsj.com"))
    article = self.new
    article.title = doc.search("h3.wsj-headline").first.text
    article.text = doc.search("p.wsj-summary").first.text
    article.url = doc.search("h3.wsj-headline").first.css("a").attr("href").value
    article
end