[英]Ruby / Nokogiri / Mechanize: How to download XLS file?
我必须请求发送文件的页面www.example.com/xls_file
。 我有Nokogiri和机械化可用。 我将如何下载文件并将其保存在本地?
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
如果要从中获取文件的站点以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.