-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper_module.rb
More file actions
39 lines (31 loc) · 851 Bytes
/
scraper_module.rb
File metadata and controls
39 lines (31 loc) · 851 Bytes
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
require 'nokogiri'
require 'open-uri'
module Scraper
def self.scrape_url url
result = []
return_text = -> (cell){ cell.nil? ? '' : cell.text.strip }
doc = Nokogiri::HTML5(URI.open(url))
if doc.errors.count > 0
errors_messenger(doc)
return
end
rows = doc.at('.wikitable').search('tbody').search('tr')
t_head = rows.shift.search('th')
rows.each do |row|
cells = row.search('td')
hash = {}
t_head.each_with_index do |col, index|
col_name = return_text.call(col)
hash[col_name.to_sym] = return_text.call(cells[index])
end
result.push(hash)
end
result
end
private
def self.errors_messenger(source)
source.errors.each { |err| puts err }
end
end
url = 'https://en.wikipedia.org/wiki/List_of_museums_in_Idaho'
puts Scraper.scrape_url(url)