Nokogiri CSS选择器,编号为类



我正在使用Nokogiri来尝试检测页面中是否有具有类似类的元素。不幸的是,这门课涉及到一个数字——我发现用Nokogiri处理这个数字很困难。问题是,Nokogiri总是在类名以数字开头时出错。

这是我得到的错误回溯:

Nokogiri::CSS::SyntaxError: unexpected '.0' after '[#<Nokogiri::CSS::Node:0x007fced40a3ef8 @type=:CONDITIONAL_SELECTOR, @value=[#<Nokogiri::CSS::Node:0x007fcecdf502a0 @type=:ELEMENT_NAME, @value=["tr"]>, #<Nokogiri::CSS::Node:0x007fced40a3f70 @type=:COMBINATOR, @value=[#<Nokogiri::CSS::Node:0x007fcecdf50188 @type=:CLASS_CONDITION, @value=["classA"]>, #<Nokogiri::CSS::Node:0x007fcecdf518d0 @type=:CLASS_CONDITION, @value=["classB"]>]>]>]'
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/nokogiri-1.6.3.rc3/lib/nokogiri/css/parser_extras.rb:87:in `on_error'
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/2.1.0/racc/parser.rb:258:in `_racc_do_parse_c'
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/2.1.0/racc/parser.rb:258:in `do_parse'
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/nokogiri-1.6.3.rc3/lib/nokogiri/css/parser_extras.rb:62:in `parse'
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/nokogiri-1.6.3.rc3/lib/nokogiri/css/parser_extras.rb:79:in `xpath_for'
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/nokogiri-1.6.3.rc3/lib/nokogiri/css.rb:23:in `xpath_for'
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/nokogiri-1.6.3.rc3/lib/nokogiri/xml/node.rb:211:in `block in css'
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/nokogiri-1.6.3.rc3/lib/nokogiri/xml/node.rb:210:in `map'
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/nokogiri-1.6.3.rc3/lib/nokogiri/xml/node.rb:210:in `css'
    from (irb):106
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/console.rb:90:in `start'
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/console.rb:9:in `start'
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:69:in `console'
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands.rb:17:in `<top (required)>'
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `require'
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `block in require'
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in `load_dependency'
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `require'
    from /Users/works/Documents/OF/RUBY-211/indoskyapi/bin/rails:9:in `<top (required)>'
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `load'
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `block in load'
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in `load_dependency'
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `load'
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Users/works/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from -e:1:in `<main>'
我使用的代码是:
pg.css("tr.classA.classB.0")      

编辑:我认为这个问题可以从我的"问题"中提取出来,即:我如何使用数字作为nokogiri .css函数中的类选择器。

这个问题和最近的另一个问题很相似。虽然这个问题是关于id的,但类的解决方案略有不同。

问题是,虽然0作为HTML类值是有效的,但它作为CSS类选择器是无效的,因为它们不能以数字开头。

你可以使用[att~=val]属性选择器来解决这个问题,像这样:

pg.css("tr.classA.classB[class~='0']")

这将匹配所有类别classA, classB0中的所有tr元素。

CSS中,标识符不能以数字开头。我从未使用过Nokogiri,但我认为它遵循这个原则,不允许你在选择器中使用这样的标识符。下面是关于CSS标识符中允许的字符的更详细的答案:https://stackoverflow.com/a/449005

相关内容

  • 没有找到相关文章

最新更新