【问题标题】:Issue with ruby parsingruby 解析的问题
【发布时间】:2013-02-25 19:56:03
【问题描述】:

我只是在用 ruby​​ 中用 nokogiri 解析网站时遇到了一点问题。

这是网站的样子

<div id="post_message_111112" class="postcontent">

        Hee is text 1 
     here is another
      </div>
<div id="post_message_111111" class="postcontent">

            Here is text 2
    </div>

这是我的解析代码

 doc = Nokogiri::HTML(open(myNewLink))
 myPost = doc.xpath("//div[@class='postcontent']/text()").to_a()

ii=0

 while ii!=myPost.length
     puts "#{ii}  #{myPost[ii].to_s().strip}"
   ii+=1
 end

我的问题是当它显示它时,由于Hee is text 1 之后的新行,to_a 让它变得很奇怪

myPost[0] = hee is text 1
myPost[1] = here is another
myPost[2] = here is text 2

我希望每个 div 都有自己的信息。喜欢

myPost[0] = hee is text 1 here is another
myPost[1] = here is text 2

如何解决这个谢谢

更新

我试过了

 myPost = doc.xpath("//div[@class='postcontent']/text()").to_a()

myPost.each_with_index do |post, index|
  puts "#{index}  #{post.to_s().gsub(/\n/, ' ').strip}"
end

我放 post.to_s().gsub 是因为它抱怨 gsub 不是一种发布方法。但我仍然有同样的问题。我知道我做错了只是毁了我的头

更新 2

忘了说新行是&lt;br /&gt;,甚至还有

   doc.search('br').each do |n|
  n.replace('')
end

doc.search('br').remove

问题依然存在

【问题讨论】:

    标签: html ruby arrays parsing nokogiri


    【解决方案1】:

    这里,让我为你清理一下:

    doc.search('div.postcontent').each_with_index do |div, i|
      puts "#{i} #{div.text.gsub(/\s+/, ' ').strip}"
    end
    # 0 Hee is text 1 here is another
    # 1 Here is text 2
    

    【讨论】:

      【解决方案2】:

      如果您查看myPost 数组,您会发现每个 div 实际上是它自己的消息。第一个恰好包含一个换行符\n。要将其替换为空格,请使用#gsub(/\n/, ' ')。所以你的循环看起来像这样:

      myPost.each_with_index do |post, index|
          puts "#{index}  #{post.to_s.gsub(/\n/, ' ').strip}"
      end
      

      编辑:

      根据我对它的有限了解,xpath只能找到节点。子节点是&lt;br /&gt;,因此它们之间有多个文本,或者您的搜索中包含div 标记。肯定有一种方法可以在&lt;br /&gt; 节点之间加入文本,但我不知道。 在你找到它之前,这里有一些有用的东西:

      1. 将您的 xpath 匹配替换为 "//div[@class='postcontent']"

      2. 调整循环以删除 div 标签:

        myPost.each_with_index do |post, index|
             post = post.to_s
             post.gsub!(/\n/, ' ')
             post.gsub!(/^<div[^>]*>/, '') # delete opening div tag
             post.gsub!(%r|</\s*div[^>]*>|, '') # delete closing div tag
             puts "#{index}  #{post.strip}"
        end
        

      【讨论】:

      • 感谢您的快速回复,但遇到了一个小问题。在 myPost = doc.xpath("//div[@class='postcontent']/text()").to_a()...我有...之后 myPost.each_with_index 做 |post, index| puts "#{index} #{post.gsub(/\n/, ' ').strip}" end.... 但是它给出了关于 post 没有方法 gsub,所以如果我放... myPost.each_with_index做 |发布,索引| puts "#{index} #{post.to_s().gsub(/\n/, ' ').strip}" 结束......它解决了没有 gsub 的问题,但仍然是数组的问题跨度>
      • 对不起,当然缺少to_s。我将其修复为原始版本,但现在它为我将每个帖子打印在一行中。我不知道到底发生了什么,你能提供一个有效的例子吗?您发布的 html 无法自行解析。
      • 文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本

        谢谢。
      • 我明白了,区别似乎是&lt;br /&gt;。在您的第一个带有换行符的示例中,一切都按预期工作,但在您的第二个示例中则不然。
      • 我该如何解决这个问题?即使 doc.search("//br").remove 仍然有问题
      猜你喜欢
      • 1970-01-01
      • 2013-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多