簡體   English   中英

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

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM