如何在我的 Nokogiri 文档中搜索和替换属性值?
How do I search and replace an attibute value in my Nokogiri doc?
如何替换我的 Nokogiri 文档中的属性值?我使用以下方法确定了相关属性:
doc.xpath("//pre[@class='text-results']").xpath("meta[@http-equiv='Content-Type']").attr("content").to_s
但是当我尝试为其分配不同的值时,我的 Rails 控制台中没有返回任何内容,这让我相信我没有做对:
2.3.0 :027 > doc.xpath("//pre[@class='text-results']").xpath("meta[@http-equiv='Content-Type']").attr("content") = "text/html; charset=iso8849";
2.3.0 :028 >
对给定答案的响应错误:
2.3.0 :005 > doc.xpath("//pre[@class='text-results']").xpath("meta[@http-equiv='Content-Type']")["content"] = "text/html; charset=iso8849"
NoMethodError: undefined method `[]=' for #<Nokogiri::XML::NodeSet:0x007f820271c628>
Did you mean? []
from (irb):5
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/console.rb:110:in `start'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/console.rb:9:in `start'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
我正在使用 Rails 4.2.7。
试试[]=
方法
node["content"] = "text/html; charset=iso8849"
如果要更新第一个
doc.xpath("//pre[@class='text-results']").xpath("meta[@http-equiv='Content-Type']").first["content"] = "text/html; charset=iso8849"
或者每个节点
doc.xpath("//pre[@class='text-results']").xpath("meta[@http-equiv='Content-Type']").each { |node| node["content"] = "text/html; charset=iso8849" }
如何替换我的 Nokogiri 文档中的属性值?我使用以下方法确定了相关属性:
doc.xpath("//pre[@class='text-results']").xpath("meta[@http-equiv='Content-Type']").attr("content").to_s
但是当我尝试为其分配不同的值时,我的 Rails 控制台中没有返回任何内容,这让我相信我没有做对:
2.3.0 :027 > doc.xpath("//pre[@class='text-results']").xpath("meta[@http-equiv='Content-Type']").attr("content") = "text/html; charset=iso8849";
2.3.0 :028 >
对给定答案的响应错误:
2.3.0 :005 > doc.xpath("//pre[@class='text-results']").xpath("meta[@http-equiv='Content-Type']")["content"] = "text/html; charset=iso8849"
NoMethodError: undefined method `[]=' for #<Nokogiri::XML::NodeSet:0x007f820271c628>
Did you mean? []
from (irb):5
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/console.rb:110:in `start'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/console.rb:9:in `start'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/davea/.rvm/gems/ruby-2.3.0/gems/railties-4.2.7.1/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
我正在使用 Rails 4.2.7。
试试[]=
方法
node["content"] = "text/html; charset=iso8849"
如果要更新第一个
doc.xpath("//pre[@class='text-results']").xpath("meta[@http-equiv='Content-Type']").first["content"] = "text/html; charset=iso8849"
或者每个节点
doc.xpath("//pre[@class='text-results']").xpath("meta[@http-equiv='Content-Type']").each { |node| node["content"] = "text/html; charset=iso8849" }