【问题标题】:rubyXL (Errno::ENOENT)rubyXL (Errno::ENOENT)
【发布时间】:2013-01-05 17:33:51
【问题描述】:

我在使用 ruby​​XL 构建的爬虫时遇到问题。它正确地遍历了我的文件系统,但我收到了(Errno::ENOENT) 错误。我检查了所有的 ruby​​XL 代码,一切似乎都检查了。我的代码附在下面 - 有什么建议吗?

/Users/.../testdata.xlsx
/Users/.../moretestdata.xlsx
/Users/.../Lab 1 Data.xlsx
/Users/Dylan/.rvm/gems/ruby-1.9.3-p327/gems/rubyXL-1.2.10/lib/rubyXL/parser.rb:404:in `initialize': No such file or directory - /Users/Dylan/.../sheet6.xml (Errno::ENOENT)
    from /Users/Dylan/.rvm/gems/ruby-1.9.3-p327/gems/rubyXL-1.2.10/lib/rubyXL/parser.rb:404:in `open'
    from /Users/Dylan/.rvm/gems/ruby-1.9.3-p327/gems/rubyXL-1.2.10/lib/rubyXL/parser.rb:404:in `block in decompress'
    from /Users/Dylan/.rvm/gems/ruby-1.9.3-p327/gems/rubyXL-1.2.10/lib/rubyXL/parser.rb:402:in `upto'
    from /Users/Dylan/.rvm/gems/ruby-1.9.3-p327/gems/rubyXL-1.2.10/lib/rubyXL/parser.rb:402:in `decompress'
    from /Users/Dylan/.rvm/gems/ruby-1.9.3-p327/gems/rubyXL-1.2.10/lib/rubyXL/parser.rb:47:in `parse'
    from xlcrawler.rb:9:in `block in xlcrawler'
    from /Users/Dylan/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/find.rb:41:in `block in find'
    from /Users/Dylan/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/find.rb:40:in `catch'
    from /Users/Dylan/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/find.rb:40:in `find'
    from xlcrawler.rb:6:in `xlcrawler'
    from xlcrawler.rb:22:in `<main>'

require 'find'
require 'rubyXL'

def xlcrawler(path)
  count = 0
  Find.find(path) do |file|                                # begin iteration of each file of a specified directory
    if file =~ /\b.xlsx$\b/                                # check if a given file is xlsx format
      puts file                                            # ensure crawler is traversing the file system
      workbook = RubyXL::Parser.parse(file).worksheets     # creates an object containing all worksheets of an excel workbook
      workbook.each do |worksheet|                         # begin iteration over each worksheet
        data = worksheet.extract_data.to_s                 # extract data of a given worksheet - must be converted to a string in order to match a regex
        if data =~ /regex/
          puts file
          count += 1
        end      
      end
    end
  end
  puts "#{count} files were found"
end

xlcrawler('/Users/')

【问题讨论】:

  • Lab 1 Data.xlsx 似乎有问题,我可能会猜测 sheet6 被隐藏或保护,或者可能以某种方式重命名,因此 .worksheets 没有正确处理
  • @Pynner 好点。该文件实际上位于我的邮件目录中——只是一堆数字和图表。但是,我确实仔细检查了工作表,没有任何内容被隐藏或保护。

标签: ruby excel web-crawler rubyxl


【解决方案1】:

我在 github 上对 ruby​​XL 代码进行了一些挖掘,看起来解压缩方法中存在错误。

  files['styles'] = Nokogiri::XML.parse(File.open(File.join(dir_path,'xl','styles.xml'),'r'))
  @num_sheets = files['workbook'].css('sheets').children.size
  @num_sheets = Integer(@num_sheets)

  #adds all worksheet xml files to files hash
  i=1
  1.upto(@num_sheets) do
    filename = 'sheet'+i.to_s # <----- BUG IS HERE
    files[i] = Nokogiri::XML.parse(File.open(File.join(dir_path,'xl','worksheets',filename+'.xml'),'r'))
    i=i+1
  end

这段代码对 excel 中的工作表编号做了一个假设,这是不正确的。此代码只是计算纸张的数量,并以数字方式分配它们。但是,如果您删除一个工作表然后创建一个新工作表,则数字序列会被破坏。

如果您检查Lab Data 1.xlsx 文件,如果您打开 vba 开发人员窗口(按 alt + F11),您会看到没有 sheet6,您应该会看到类似

如您所见,这种安排会破坏 for 循环并在 i = 6 时引发异常。

【讨论】:

  • 我对您的洞察力非常感谢!你死了 - 我的 excel 文件中没有第 6 表。我在 github 上创建了一个问题;现在修复该死的东西:)
猜你喜欢
  • 2017-05-29
  • 2021-07-14
  • 2019-10-20
  • 2020-05-14
  • 2012-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-02
相关资源
最近更新 更多