简体   繁体   English

Ruby / Nokogiri / Mechanize:如何下载XLS文件?

[英]Ruby / Nokogiri / Mechanize: How to download XLS file?

I have to make a request to a page www.example.com/xls_file which sends a file. 我必须请求发送文件的页面www.example.com/xls_file I have Nokogiri and Mechanize available. 我有Nokogiri和机械化可用。 How would I download the file and save it locally? 我将如何下载文件并将其保存在本地?

def file
  grab_file if !File.exists?("sales_data.csv")
  File.open("sales_data.csv")
end

def grab_file
  # What do I do here?
  # Nokogiri::HTML(open("http://www.example.com/xls_file"))
end
require 'open-uri'

File.open('any_name_here.xls', 'wb') do |file|
 file << open('http://www.example.com/xls_file.xls').read
end

If the site you want to get the file from starts with https:// then you might want to add the following things to avoid Ruby reporting SSL errors: 如果要从中获取文件的站点以https://开头,那么您可能需要添加以下内容以避免Ruby报告SSL错误:

require 'open-uri'
require 'openssl'

File.open('any_name_here.xls', 'wb') do |file|
  file << open('https://www.example.com/xls_file.xls', ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE).read
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM