CSS选择器用于处理多种可能性时遇到困难。

huangapple go评论96阅读模式
英文:

Having difficulty with CSS selector for multiple possibilities

问题

I'm having difficulty getting a CSS selector to work with Nokogiri. I'm trying
to search for all <tr> nodes that are direct descendants of a
<table> or are direct descendants of a <thead> that is a direct
descendants of a <table>. The following code will clarify the issue:

#!/usr/bin/ruby -w
require 'nokogiri'

puts RUBY_VERSION #=> 2.7.0
puts Nokogiri::VERSION #=> 1.13.6

html = <<~HTML
<html>
    <body>
    
        <table>
            <thead>
                <tr>
                    <th>city</th>
                    <th>state</th>
                    <th>classification</th>
                </tr>
            </thead>
            
            <tr>
                <th>Blacksburg</th>
                <td>Virginia</td>
                <td>College</td>
            </tr>
        </table>
        
    </body>
</html>
HTML

doc = Nokogiri::HTML(html)
table = doc.at('table')

puts table.search('> tr').length #=> 1
puts table.search('> thead > tr').length #=> 1
puts table.search('> tr, > thead > tr').length #=> barf

The first two searches work. The third search is what I actually want to do, but
unless I've really forgotten how CSS selectors work (entirely possible) then it
seems to me that third selector should work too. Instead I get this error
message:

Traceback (most recent call last):
	12: from ./parse.rb:36:in `<main>'
	11: from /var/lib/gems/2.7.0/gems/nokogiri-1.13.6-x86_64-linux/lib/nokogiri/xml/searchable.rb:54:in `search'
	10: from /var/lib/gems/2.7.0/gems/nokogiri-1.13.6-x86_64-linux/lib/nokogiri/xml/searchable.rb:54:in `map'
	 9: from /var/lib/gems/2.7.0/gems/nokogiri-1.13.6-x86_64-linux/lib/nokogiri/xml/searchable.rb:55:in `block in search'
	 8: from /var/lib/gems/2.7.0/gems/nokogiri-1.13.6-x86_64-linux/lib/nokogiri/xml/searchable.rb:245:in `xpath_query_from_css_rule'
	 7: from /var/lib/gems/2.7.0/gems/nokogiri-1.13.6-x86_64-linux/lib/nokogiri/xml/searchable.rb:245:in `map'
	 6: from /var/lib/gems/2.7.0/gems/nokogiri-1.13.6-x86_64-linux/lib/nokogiri/xml/searchable.rb:246:in `block in xpath_query_from_css_rule'
	 5: from /var/lib/gems/2.7.0/gems/nokogiri-1.13.6-x86_64-linux/lib/nokogiri/css.rb:46:in `xpath_for'
	 4: from /var/lib/gems/2.7.0/gems/nokogiri-1.13.6-x86_64-linux/lib/nokogiri/css/parser_extras.rb:78:in `xpath_for'
	 3: from /var/lib/gems/2.7.0/gems/nokogiri-1.13.6-x86_64-linux/lib/nokogiri/css/parser_extras.rb:68:in `parse'
	 2: from (eval):3:in `do_parse'
	 1: from (eval):3:in `_racc_do_parse_c'
/var/lib/gems/2.7.0/gems/nokogiri-1.13.6-x86_64-linux/lib/nokogiri/css/parser_extras.rb:86:in `on_error': unexpected '>' after ',' (Nokogiri::CSS::SyntaxError)

Ruby version is 2.7.0 and Nokogiri is 1.13.6. Any idea what I'm doing wrong?

英文:

I'm having difficulty getting a CSS selector to work with Nokogiri. I'm trying
to search for all &lt;tr&gt; nodes that are direct descendants of a
&lt;table&gt; or are direct descendants of a &lt;thead&gt; that is a direct
descendants of a &lt;table&gt;. The following code will clarify the issue:

#!/usr/bin/ruby -w
require &#39;nokogiri&#39;

puts RUBY_VERSION #=&gt; 2.7.0
puts Nokogiri::VERSION #=&gt; 1.13.6

html = &lt;&lt;~&#39;HTML&#39;
&lt;html&gt;
    &lt;body&gt;
    
        &lt;table&gt;
            &lt;thead&gt;
                &lt;tr&gt;
                    &lt;th&gt;city&lt;/th&gt;
                    &lt;th&gt;state&lt;/th&gt;
                    &lt;th&gt;classification&lt;/th&gt;
                &lt;/tr&gt;
            &lt;/thead&gt;
            
            &lt;tr&gt;
                &lt;th&gt;Blacksburg&lt;/th&gt;
                &lt;td&gt;Virginia&lt;/td&gt;
                &lt;td&gt;College&lt;/td&gt;
            &lt;/tr&gt;
        &lt;/table&gt;
        
    &lt;/body&gt;
&lt;/html&gt;
HTML

doc = Nokogiri::HTML(html)
table = doc.at(&#39;table&#39;)

puts table.search(&#39;&gt; tr&#39;).length #=&gt; 1
puts table.search(&#39;&gt; thead &gt; tr&#39;).length #=&gt; 1
puts table.search(&#39;&gt; tr, &gt; thead &gt; tr&#39;).length #=&gt; barf

The first two searches work. The third search is what I actually want to do, but
unless I've really forgotten how CSS selectors work (entirely possible) then it
seems to me that third selector should work too. Instead I get this error
message:

Traceback (most recent call last):
	12: from ./parse.rb:36:in `&lt;main&gt;&#39;
	11: from /var/lib/gems/2.7.0/gems/nokogiri-1.13.6-x86_64-linux/lib/nokogiri/xml/searchable.rb:54:in `search&#39;
	10: from /var/lib/gems/2.7.0/gems/nokogiri-1.13.6-x86_64-linux/lib/nokogiri/xml/searchable.rb:54:in `map&#39;
	 9: from /var/lib/gems/2.7.0/gems/nokogiri-1.13.6-x86_64-linux/lib/nokogiri/xml/searchable.rb:55:in `block in search&#39;
	 8: from /var/lib/gems/2.7.0/gems/nokogiri-1.13.6-x86_64-linux/lib/nokogiri/xml/searchable.rb:245:in `xpath_query_from_css_rule&#39;
	 7: from /var/lib/gems/2.7.0/gems/nokogiri-1.13.6-x86_64-linux/lib/nokogiri/xml/searchable.rb:245:in `map&#39;
	 6: from /var/lib/gems/2.7.0/gems/nokogiri-1.13.6-x86_64-linux/lib/nokogiri/xml/searchable.rb:246:in `block in xpath_query_from_css_rule&#39;
	 5: from /var/lib/gems/2.7.0/gems/nokogiri-1.13.6-x86_64-linux/lib/nokogiri/css.rb:46:in `xpath_for&#39;
	 4: from /var/lib/gems/2.7.0/gems/nokogiri-1.13.6-x86_64-linux/lib/nokogiri/css/parser_extras.rb:78:in `xpath_for&#39;
	 3: from /var/lib/gems/2.7.0/gems/nokogiri-1.13.6-x86_64-linux/lib/nokogiri/css/parser_extras.rb:68:in `parse&#39;
	 2: from (eval):3:in `do_parse&#39;
	 1: from (eval):3:in `_racc_do_parse_c&#39;
/var/lib/gems/2.7.0/gems/nokogiri-1.13.6-x86_64-linux/lib/nokogiri/css/parser_extras.rb:86:in `on_error&#39;: unexpected &#39;&gt; &#39; after &#39;, &#39; (Nokogiri::CSS::SyntaxError)

Ruby version is 2.7.0 and Nokogiri is 1.13.6. Any idea what I'm doing wrong?

答案1

得分: 0

以下是翻译好的内容:

自然而然,直到我首先向某人提问,我才找到答案。这是一种奇怪的终身模式。无论如何,我将发布答案以供后人参考。答案是要为搜索提供多个参数:

puts table.search('> tr', '> thead > tr').length
英文:

Well naturally I couldn't find the answer myself until I asked someone first. It's a bizarre lifelong pattern. Anyway, I'll post the answer for posterity. The answer is to give multiple arguments to search:

puts table.search(&#39;&gt; tr&#39;, &#39;&gt; thead &gt; tr&#39;).length

huangapple
  • 本文由 发表于 2023年4月13日 19:35:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004945.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定