Ruby 多次输出

Ruby 多次输出,ruby,nokogiri,Ruby,Nokogiri,我有以下代码: require 'rubygems' require 'nokogiri' require 'open-uri' time = Time.new url = "http://mobile.bahn.de/bin/mobil/bhftafel.exe/dox?input=Richard-Strauss-Stra%DFe%2C+M%FCnchen%23625127&date=" + time.strftime("%d%m%Y") + "&time=" +

我有以下代码:

require 'rubygems'
require 'nokogiri'
require 'open-uri'

time = Time.new

url = "http://mobile.bahn.de/bin/mobil/bhftafel.exe/dox?input=Richard-Strauss-Stra%DFe%2C+M%FCnchen%23625127&date=" + 
  time.strftime("%d%m%Y") +
  "&time=" +
  time.strftime("%H") +
  "%3A" +
  time.strftime("%M") +
  "&productsFilter=1111111111000000&REQTrain_name=&maxJourneys=10&start=Suchen&boardType=Abfahrt&ao=yes"

doc = Nokogiri::HTML(open(url))
doc.xpath('//div//p').remove
doc.encoding = 'UTF-8'
doc = doc.xpath('//div').each do |node|
  text = node.text.gsub(/\n([ \t]*\n)+/,"\n",).gsub(/^\s+|\s+$/,'').gsub("Startseite", '').gsub("Impressum", '')
  puts text unless text.empty?
end
我有两个问题:

  • 代码输出三次,而不是一次
  • 德语中的“umlauts”类似于
    äü

  • 原始HTML很长而且没有缩进,因此很难调试

    但我认为你需要替换:

    doc = doc.xpath('//div').each do |node|
    
    与:

    第一个还包括所有
    元素,因此它包括
    //body/div
    ,然后分别包括
    //body/div
    中的
    s

    使用
    puts
    ,我对umlaut字符没有任何问题,但在将它们写入文件时遇到了问题。你到底有什么问题?最好为umlauts问题创建一个关于堆栈溢出的新问题

    doc = doc.xpath('//body/div').each do |node|