Nokogiri CSS选择器,数字为类

时间:2014-08-05 12:38:57

标签: ruby nokogiri

我正在使用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函数中使用数字作为类选择器。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

此问题与another recent question类似。这个问题问及id s,但是类的解决方案略有不同。

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

您可以使用[att~=val] attribute selector这样解决此问题:

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

这将匹配所有类trclassAclassB中的所有0元素。