文件系统爬虫 – 迭代错误
我目前正在使用以下代码构建文件系统爬网程序:
require 'find' require 'spreadsheet' Spreadsheet.client_encoding = 'UTF-8' count = 0 Find.find('/Users/Anconia/crawler/') do |file| if file =~ /\b.xls$/ # check if filename ends in desired format contents = Spreadsheet.open(file).worksheets contents.each do |row| if row =~ /regex/ puts file count += 1 end end end end puts "#{count} files were found"
我收到以下输出: 0 files were found
正则表达式经过测试和更正 – 我目前在另一个可行的爬虫中使用它。
row.inspect
的输出是
#<Spreadsheet::Excel::Worksheet:0x003ffa5d418538 @row_addresses= @default_format= @selected= @dimensions= @name=Sheet1 @workbook=# @rows=[] @columns=[] @links={} @merged_cells=[] @protected=false @password_hash=0 @changes={} @offsets={} @reader=# @ole=# @offset=15341 @guts={} @rows[3]>
– 当然没有什么要迭代的。
试试这个:
content = Spreadsheet.open(file) sheet = content.worksheet 0 sheet.each do |row| ...
正如迭戈所提到的,我应该一直在迭代内容 – 非常感谢澄清! 还应注意,在进行任何迭代之前,必须将row
转换为字符串。