-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelpais.rb
57 lines (47 loc) · 1.28 KB
/
elpais.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require 'tweakphoeus'
require 'nokogiri'
class Elpais
URL = "http://elpais.com"
URL_SOC = "#{URL}/tag/sociedad/a/"
def initialize
@http = Tweakphoeus::Client.new()
end
def get_notices
notices = {}
url = ""
page = get_page(URL_SOC)
puts last_page?(page)
File.open('notices.txt', 'a') do |f|
while last_page?(page)
table = page.css('.columna_principal')
table.css('.article.estirar').each do |notice|
date = notice.css('.fecha').text
link = notice.css('h2 > a').attr("href").text
title = notice.css('h2 > a').text
desc = notice.css('p').text
notices = {
"date" => date,
"link" => link,
"title" => title,
"desc" => desc
}
f.puts notices
end
url = next_page(page)
page = get_page(url)
end
end
end
def get_page url
response = @http.get(url)
Nokogiri::HTML(response.body)
end
def next_page page
URL + page.css('.paginacion > .boton.activo').first.attr('href')
end
def last_page? page
puts page.css('.paginacion > .boton.activo').first.attr('title')
return true if page.css('.paginacion > .boton.activo').first.attr('title').eql?("Página siguiente")
false
end
end