Class: RubyChan::Scraper
- Inherits:
-
Object
- Object
- RubyChan::Scraper
- Defined in:
- lib/scraper.rb
Instance Method Summary collapse
-
#initialize(uri) ⇒ Scraper
constructor
A new instance of Scraper.
- #scrape ⇒ Object
Constructor Details
#initialize(uri) ⇒ Scraper
Returns a new instance of Scraper.
7 8 9 |
# File 'lib/scraper.rb', line 7 def initialize(uri) @uri = uri end |
Instance Method Details
#scrape ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/scraper.rb', line 11 def scrape doc = Nokogiri::HTML(open(@uri)) doc.xpath('//a').each do |link| if(link['href'] =~ /\/\/images.4chan.org\/.+\/src\/\d+\..../) uri = URI(link['href']) puts "Downloading #{uri}" Net::HTTP.start(uri.host) do |http| resp = http.get(uri.path) open(File.basename(uri.path), "wb") do |file| file.write(resp.body) end end end end end |