我正在嘗試創建一個基本的ruby scraper,它將從html源代碼中獲取8個字母或更長的單詞。然後將它們保存在與單詞的第一個字符對應的文件中。看起來很簡單吧?Ruby將打開文件但不寫入它?拉我的頭髮
re = /\w{8,}/
cre = /[a-z0-9]/
a = b.html #This grabs the html from the browser
matchx = a.scan(re)
matchx.each do |xx|
word = xx.to_s.downcase.chomp
fchar = word[0].chr
if (fchar.match(cre)) #Not sure if I need this
@pcount += 1
fname = @WordsFName+fchar #@WordsFName is a prefix
tmpF = File.open(fname,"a+")
#Check for duplicates, if not write to file
exists = File.readlines(fname).any? { |li| li[word] }
if (!exists)
tmpF.write(word+"\n")
print word
@wcount += 1
end
end
end
Ruby成功抓取所有單詞,獲取第一個字符,並打開所有必需的文件,但未能寫入它。此外,打印方法打印包括重複的所有單詞,但檢查任何?在irb上的方法沒有給出任何問題..
您是否收到錯誤? – scaganoff 2012-04-19 02:32:36