首页 > 其他 > 详细

Ruby批量下载音乐

时间:2014-02-28 18:23:20      阅读:484      评论:0      收藏:0      [点我收藏+]

下载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(‘ ‘,‘_‘)
       begin
       mp3 = doc.css(‘div#menubar a‘)[0][‘href‘] #mp3
       download(mp3,name)
       writefile(txt,name)
       rescue
       end
   end
end


Ruby批量下载音乐,布布扣,bubuko.com

Ruby批量下载音乐

原文:http://abian.blog.51cto.com/751059/1364636

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!