如何使用ruby下载许多xml文件

时间:2012-04-08 13:28:44

标签: ruby nokogiri net-http

我想从网站下载一堆xml文件。在这里,我创建了一个小的nokogiri脚本,它收集所有链接,然后将它们存储在一个数组中:

require 'rubygems'
require 'nokogiri'
require 'open-uri'

@urls = []
# The webbsite that have all the xml-links
doc = Nokogiri::HTML(open('http://testnavet.skolverket.se/SusaNavExport/EmilExporter?GetEvent&EMILVersion=1.1&NotExpired&EEFormOfStudy=normal&EIAcademicType=UoH&SelectEI'))
# Grab all the links and store it in 
doc.xpath('//a/@href').each do |links|
   @urls << links.content
end

每个链接都是一个xml文件,如何将每个xml文件存储在一个文件夹中? 我对此的解决方案,但它冻结了:

@urls.length.times do |i|
   Net::HTTP.start(i) do |http|
   resp = http.get("/.xml")
   open("/files", "wb") do |file|
      file.write(resp.body)
   end
 end
end

1 个答案:

答案 0 :(得分:0)

而不是

@urls.length.times do |i|
   Net::HTTP.start(i) do |http|
   resp = http.get("/.xml")
   open("/files", "wb") do |file|
      file.write(resp.body)
   end
 end
end

你不应该这样做:

@urls.each do |url|
  ...
  http.get(url)
  ...
end