Class: Simple::Scraper::Parser
- Inherits:
-
Object
- Object
- Simple::Scraper::Parser
- Defined in:
- lib/simple/scraper/parser.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Instance Method Summary collapse
-
#initialize(attributes) ⇒ Parser
constructor
A new instance of Parser.
- #parse(urls, query: {}, headers: {}) ⇒ Object
Constructor Details
#initialize(attributes) ⇒ Parser
Returns a new instance of Parser.
6 7 8 |
# File 'lib/simple/scraper/parser.rb', line 6 def initialize(attributes) @attributes = attributes || {} end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
4 5 6 |
# File 'lib/simple/scraper/parser.rb', line 4 def attributes @attributes end |
Instance Method Details
#parse(urls, query: {}, headers: {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/simple/scraper/parser.rb', line 10 def parse(urls, query: {}, headers: {}) Parallel.map(Array(urls), in_threads: Simple::Scraper.configuration.number_of_threads) do |url| Finder.find(url: url, query: query, headers: headers) do |page| attributes.each_with_object({}) do |(key, ), hsh| hsh[key] = [:handler].call(page.xpath([:selector])) rescue StandardError => e Simple::Scraper.logger&.error e hsh[key] = [:default] end end end.compact end |