我正在使用Nokogiri作为工具来帮助我完成一项我正在上学的项目。现在,该项目的想法是在HTML文档中搜索用户提供的关键字。该函数将在<body>
元素之后的HTML字符串中查找所有出现的关键字,除非关键字出现在HTML标记内,然后将带有标记的字符串包围为“突出显示”关键字。现在我的代码失败了一个非常大的错误调用堆栈。主要错误是这样的:
引入nokogiri :: CSS ::语法错误:意想不到&#39; $&#39;后&#39;&#39; [:substring_match,&#34;简单&#34;]&#39;(引入nokogiri :: CSS ::的SyntaxError )
我当前的方法:
require 'nokogiri'
file = File.open ("desktop/Personal/code.HTML")
@doc = Nokogiri::HTML(file)
puts @doc
puts "Welcome to the HTML keyword highlighter!"
puts "Please enter a keyword"
keyword = gets.chomp
highlight = "<span style='background-color: yellow; color: black'>#{keyword}</span>"
search = @doc.xpath("//body").search("[text()*=#{keyword}")
search.each do |node|
nodeN = node.name
nodeH = node.content.gsub(keyword, highlight)
node.replace(nodeH)
end
file.close
错误调用堆栈:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2. 0.0/gems/nokogiri-1.5.6/lib/nokogiri/css/parser_extras.rb:87:in `on_error': unexpected '$' after '[:substring_match, "simple"]' (Nokogiri::CSS::SyntaxError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/racc/parser.rb:258:in `_racc_do_parse_c'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/racc/parser.rb:258:in `do_parse'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/css/parser_extras.rb:62:in `parse'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/css/parser_extras.rb:79:in `xpath_for'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/css.rb:23:in `xpath_for'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:111:in `block (2 levels) in css'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:109:in `map'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:109:in `block in css'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:239:in `block in each'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:238:in `upto'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:238:in `each'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:105:in `css'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:81:in `block in search'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:80:in `each'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0/gems/nokogiri-1.5.6/lib/nokogiri/xml/node_set.rb:80:in `search'
from /Users/Eva/Desktop/Personal/Project_Nokogiri.rb:21:in `<main>'
关于如何防止这种情况的任何建议都将受到真正的赞赏!我是编程,Ruby和Nokogiri的新手,所以我很少理解如何自己解决这个问题。对不起格式化不好。谢谢你的帮助!
答案 0 :(得分:1)
您忘记关闭方括号:
search = @doc.xpath("//body").search("[text()*=\"#{keyword}\"]")
我会添加引号以防万一:
keyword
(您可以通过确保双引号在{{1}}中进行转义以使其具有防弹性来扩展它)