# coding: utf-8
require 'open-uri'
require 'nokogiri'
require 'pp'
# ./images ディレクトリがなかったら作る
save_dir = 'images'
Dir.mkdir(save_dir) unless File.exists?(save_dir)
doc = Nokogiri::HTML(open('http://blog.livedoor.jp/insidears/archives/52516977.html'))
image_urls = []
doc.css('.main a').each do |link|
image_urls.push({ :url => link[:href], :title => link[:title] })
end
pp image_urls
image_urls.each_with_index do |image_url,index|
pp image_url
image_data = open(image_url[:url])
File.open("#{save_dir}/#{image_url[:title]}.jpg",'w') do |f|
f.write image_data.read
end
end
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.