生活随笔
收集整理的這篇文章主要介紹了
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 txtaFile.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 txtname = doc.css('div#title').text.strip.gsub(' ','_')#name = doc.css('div#title').text.encode("GBK") 如果想保存中文名,就用String#encode方法beginmp3 = doc.css('div#menubar a')[0]['href'] #mp3download(mp3,name)writefile(txt,name)rescueendend
end
轉載于:https://blog.51cto.com/abian/1364636
總結
以上是生活随笔為你收集整理的Ruby批量下载音乐的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。