Ruby大量ダウンロード音楽


VOA英語サイトの関連内容をダウンロードします。脚本は以下の通りです。
Nokogiriライブラリを使います。このライブラリの使用に関する紹介
#!/usr/bin/ruby
require 'open-uri'
require 'nokogiri'
www = 'http://www.51voa.com'
pagelist = []
doc = Nokogiri::HTML(open(www + '/Learn_A_Word_1.html'))
doc.css('div#pagelist a').each{|x| pagelist << x['href']}
def get_child_page(address)
  list = []
  doc = Nokogiri::HTML(open(address))
  doc.css('div#list a').each{|x| list << x['href']}
  list
end
def download(mp3,file)
File.open(file + ".mp3",'wb'){ |f| f.write(open(mp3).read) }
end
def writefile(txt,file)
  aFile = File.new(file + ".txt","w")
  aFile.puts txt
  aFile.close
end
pagelist.each do |address|
txt, mp3, name = ''
   list = get_child_page(www + "/" + address)
   list.each do |result|
       doc = Nokogiri::HTML(open(www + result))
       txt = doc.css('div#content').text #file txt
       name = doc.css('div#title').text.strip.gsub(' ','_')
       #name = doc.css('div#title').text.encode("GBK")         ,  String#encode  
       begin
       mp3 = doc.css('div#menubar a')[0]['href'] #mp3
       download(mp3,name)
       writefile(txt,name)
       rescue
       end
   end
end