我在一个简单的程序上遇到了我认为与 Tempfiles 相关的问题。我正在使用“open-uri”和“nokogiri”,并且正在尝试对文档进行正则表达式搜索以及使用 nokogiri 进行 xpath 搜索。但是,如果不对文档提出两个单独的请求并因此创建两个单独的临时文件,我似乎无法做到这一点。这有效,但提出了两个请求:
require 'open-uri'
require 'nokogiri'
source_url = "http://foo.com/"
#grab html document and assign it a variable
doc = open(source_url)
#grab html document, convert to Nokogiri object and assign to variable.
noko_doc = Nokogiri::HTML(open(source_url))
#create array of stuff.
foo = noko_doc.xpath("//some element").collect { |e| e }
#create another array of stuff
bar = []
doc.each do |f|
f.each do |line|
abstract_matches = line.scan(/some regex string/)
unless abstract_matches.empty?
abstract_matches.collect! do |item|
if item.to_s.match(/yet another regex string/)
item
end
end.compact!
unless abstract_matches.empty?
abstract_matches.each { |match| bar << "#{ match } / " }
end
end
end
end
#all for this
puts foo + bar
如果我可以将“doc”变量传递到 Nokogiri::HTML 并对其进行迭代,我会更喜欢。帮助?