forked from heltmm/simple_scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
53 lines (48 loc) · 1.43 KB
/
app.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
require("bundler/setup")
require 'open-uri'
require 'pry'
Bundler.require(:default)
Dir[File.dirname(__FILE__) + '/lib/*.rb'].each { |file| require file }
# html_data = open('http://web.archive.org/web/20090220003702/http://www.sitepoint.com/').read
# html_data = open('https://www.reddit.com/').read
#
# nokogiri_object = Nokogiri::HTML(html_data)
# using xPath
# tagcloud_elements = nokogiri_object.xpath("//ul[@class='tagcloud']/li/a")
# reddit_top_posts = nokogiri_object.xpath("//p[@class='title']/a")
# using css selectors
# tagcloud_elements = nokogiri_object.css("ul.tagcloud > li > a")
# reddit_top_posts.each do |post|
# puts post.text
# to get the attribute value of href
# puts tagcloud_element['href']
# end
get('/') do
erb(:index)
end
get('/reddit') do
@posts = Post.all
erb(:reddit)
end
post('/reddit') do
reddit_nokogiri_object = Nokogiri::HTML(open('https://www.reddit.com/').read)
top_posts = reddit_nokogiri_object.xpath("//p[@class='title']/a")
top_posts.each do |post|
Post.create({:title => post.text, :link => post['href']})
end
@posts = Post.all
erb(:reddit)
end
get('/aww') do
@awws = Aww.all
erb(:aww)
end
post('/aww') do
reddit_nokogiri_object = Nokogiri::HTML(open('https://www.reddit.com/r/aww').read)
top_awws = reddit_nokogiri_object.xpath("//p[@class='title']/a")
top_awws.each do |aww|
Aww.create({:title => aww.text, :link => aww['href']})
end
@awws = Aww.all
erb(:aww)
end