Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何找到以空格结尾的HTML类名?_Ruby_Nokogiri - Fatal编程技术网

Ruby 如何找到以空格结尾的HTML类名?

Ruby 如何找到以空格结尾的HTML类名?,ruby,nokogiri,Ruby,Nokogiri,我有一个HTML页面: <li id="user_432232" class="profile "> <section class="vcard clearfix"> <div class="text"> <div class="name"> <h2 class="n fn"> <a href="#" class="profile-link">Johww</a>

我有一个HTML页面:

<li id="user_432232" class="profile ">
  <section class="vcard clearfix">
    <div class="text">
      <div class="name">
      <h2 class="n fn">
        <a href="#" class="profile-link">Johww</a>
      </h2>

<div class="like-action like-action-user-432232">
  <div class="like" style=";">
    <span class="like-number" title="25 people like Jose">25</span>
  </div>
</div>
    </div>
      <p class="title">SCR</p>
    </div>
  </section>
</li>
<li id="user_432232" class="profile ">
  <section class="vcard clearfix">
    <div class="text">
      <div class="name">
      <h2 class="n fn">
        <a href="#" class="profile-link">Jose </a>
      </h2>

<div class="like-action like-action-user-432232">
  <div class="like" style=";">
    <span class="like-number" title="25 people like Jose">25</span>
  </div>
</div>
    </div>
      <p class="title">SCRT</p>
    </div>
  </section>
</li>

doc.css('.profile')
返回一个空白数组时,我得到的数据是空白的,因为
class=“profile”
以一个空格结尾,所以我无法得到该数据。

需要
class
参数中的空格,并且工作正常:

require 'nokogiri'

html = <<EOT
<html>
  <body>
    <p class="foo ">found foo</p>
    <p class="foo bar">found bar</p>
  </body>
</html>
EOT

doc = Nokogiri::HTML(html)
doc.at('.foo').to_html # => "<p class=\"foo \">found foo</p>"
doc.search('.foo').to_html # => "<p class=\"foo \">found foo</p><p class=\"foo bar\">found bar</p>"
doc.at('.bar').to_html # => "<p class=\"foo bar\">found bar</p>"
需要“nokogiri”
html=“

found-foo

found-bar

” doc.at('.bar')。to_html#=>“

找到了bar

请注意,Nokogiri在前两次检查中都找到了
.foo
,在最后一次检查中也找到了
.bar


所有类都包含一个嵌入空间。

您确定该空间有问题吗?我试过了,效果很好。可能是其他代码行之一导致了问题。例如,
item.at_css(“n fn”)
将不返回任何内容,因为没有
n
fn
节点。为什么不能将代码更改为“class='profile'”?您的结论不正确。由于其他原因,您的数据为空。我至少可以看到一个错误:您的名称选择器错误。
require 'nokogiri'

html = <<EOT
<html>
  <body>
    <p class="foo ">found foo</p>
    <p class="foo bar">found bar</p>
  </body>
</html>
EOT

doc = Nokogiri::HTML(html)
doc.at('.foo').to_html # => "<p class=\"foo \">found foo</p>"
doc.search('.foo').to_html # => "<p class=\"foo \">found foo</p><p class=\"foo bar\">found bar</p>"
doc.at('.bar').to_html # => "<p class=\"foo bar\">found bar</p>"