【发布时间】:2011-11-07 01:18:14
【问题描述】:
我在一个简单的程序上遇到了一个问题,我认为它与 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 并对其进行迭代,我会更愿意。帮忙?
【问题讨论】:
标签: ruby-on-rails ruby parsing nokogiri open-uri