下载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另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。