【问题标题】:german "umlauts"德语“变音符号”
【发布时间】:2012-02-09 18:37:09
【问题描述】:

我对这段代码有一个问题,我得到了错误的 html 编码的德语变音符号:

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

【问题讨论】:

  • “得到错误的 html 编码的德语变音符号”是什么意思?如果您在终端窗口中显示它们,它们可能是正确的,但终端无法识别 UTF-8。

标签: html parsing internationalization nokogiri


【解决方案1】:

你的代码结果在我的电脑上是完美的。所有的元音变音都正确显示,不是作为 HTML 实体,而是作为真正的元音变音。也许检查你的 Ruby 版本:Ruby 1.8 有非 ASCII 字符的问题,Ruby 1.9 修复了这个问题。

【讨论】:

    猜你喜欢
    • 2016-01-21
    • 2013-02-09
    • 1970-01-01
    • 2021-09-28
    • 2013-12-25
    • 2016-06-16
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多