【问题标题】:How to work around the invalid byte sequence in UTF-8 ArgumentError?如何解决 UTF-8 ArgumentError 中的无效字节序列?
【发布时间】:2011-05-31 02:20:19
【问题描述】:

我正在尝试运行以下代码,其中我使用 nokogiri 解析 xml 文件。我想从文本中消除换行符 包含在标签之间。我在这里的代码曾经可以工作,但由于某种原因,现在不行了。可能是因为我 升级到 ruby​​-1.9.1。

titles = node.search('b')
titles.each do |e|
  unless e.parent.name == "h4"
    if e.children.children.first.nil? == false
      puts e.children.children.first.text.gsub("\n","")
    end
  end
end

当我运行代码时出现此错误:

HI.  You're using libxml2 version 2.6.16 which is over 4 years old and has
plenty of bugs.  We suggest that for maximum HTML/XML parsing pleasure, you
upgrade your version of libxml2 and re-install nokogiri.  If you like using
libxml2 version 2.6.16, but don't like this warning, please define the constant
I_KNOW_I_AM_USING_AN_OLD_AND_BUGGY_VERSION_OF_LIBXML2 before requring nokogiri.

test.rb:35:in `gsub': invalid byte sequence in UTF-8 (ArgumentError)

【问题讨论】:

  • 你愿意不听从建议升级吗?
  • 您真的应该遵循建议(升级,而不是隐藏警告!)然后再次测试您的代码。如果错误仍然存​​在,请发表评论。顺便说一句,您还应该使用 Ruby 1.9.2 而不是 1.9.1!
  • 在哪里可以找到在 Mac OSX 中升级 ruby​​ 的说明?据我所知,我只能通过 RVM 来做到这一点
  • 混用 Mac OSX 的 Ruby 可能不是一个好主意。我会选择 MacPorts/Fink 或者 Homebrew。

标签: ruby nokogiri


【解决方案1】:

您可以尝试通过 RVM 安装 1.9.2。

curl -L https://get.rvm.io | bash
rvm install 1.9.2

如果您希望 ruby​​ 默认为您的 rvm 1.9.2 安装,那么

rvm use 1.9.2 --default

注意:以上等价于:

curl -L https://get.rvm.io | bash -s -- --ruby=1.9.2

【讨论】:

    猜你喜欢
    • 2015-07-04
    • 2016-07-05
    • 2013-08-20
    • 1970-01-01
    • 2012-06-19
    • 2017-03-31
    • 2012-12-11
    • 2023-04-03
    • 2022-10-05
    相关资源
    最近更新 更多