下载VOA英语网站的相关内容,脚本如下

要用到 Nokogiri 库。此库相关使用介绍


#!/usr/bin/rubyrequire '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']} listenddef download(mp3,file)File.open(file + ".mp3",'wb'){ |f| f.write(open(mp3).read) }enddef writefile(txt,file) aFile = File.new(file + ".txt","w") aFile.puts txt aFile.closeendpagelist.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 endend