gpt4 book ai didi

ruby - 在使用 Mechanize 进行抓取时,我总是在 Ruby 2.0 中遇到 UndefinedConversionError

转载 作者:太空宇宙 更新时间:2023-11-03 16:52:19 26 4
gpt4 key购买 nike

当我尝试使用 Mechanize 和 Ruby 2.0 提交文本区域时,我总是得到一个

Encoding::UndefinedConversionError: U+0151 from UTF-8 to ISO-8859-1

然后我尝试用 Iconv 转换文本,我得到了类似的结果:

Iconv.iconv("LATIN1", "UTF-8", text)

我收到此错误消息:

Iconv::IllegalSequence: "őzködik, melyet "...

由于文本包含东欧字符。我该怎么做才能避免这种不便或如何在不同编码之间正确转换?

最佳答案

我找到了一个优雅的解决方案:

replacements = [["À", "À"], ["Á", "Á"], ["Â", "Â"], ["Ã", "Ã"], ["Ä", "Ä"], ["Å", "Å"], ["Æ", "Æ"], ["Ç", "Ç"], ["È", "È"], ["É", "É"], ["Ê", "Ê"], ["Ë", "Ë"], ["Ì", "Ì"], ["Í", "Í"], ["Î", "Î"], ["Ï", "Ï"], ["Ð", "Ð"], ["Ñ", "Ñ"], ["Ò", "Ò"], ["Ó", "Ó"], ["Ô", "Ô"], ["Õ", "Õ"], ["Ö", "Ö"], ["Ø", "Ø"], ["Ù", "Ù"], ["Ú", "Ú"], ["Û", "Û"], ["Ü", "Ü"], ["Ý", "Ý"], ["Þ", "Þ"], ["ß", "ß"], ["à", "à"], ["á", "á"], ["â", "â"], ["ã", "ã"], ["ä", "ä"], ["å", "å"], ["æ", "æ"], ["ç", "ç"], ["è", "è"], ["é", "é"], ["ê", "ê"], ["ë", "ë"], ["ì", "ì"], ["í", "í"], ["î", "î"], ["ï", "ï"], ["ð", "ð"], ["ñ", "ñ"], ["ò", "ò"], ["ó", "ó"], ["ô", "ô"], ["õ", "õ"], ["ö", "ö"], ["ø", "ø"], ["ù", "ù"], ["ú", "ú"], ["û", "û"], ["ü", "ü"], ["ý", "ý"], ["þ", "þ"], ["ÿ", "ÿ"]]

def replace(str,replacements)
replacements.each {|replacement| str.gsub!(replacement[0], replacement[1])}
return str
end

my_string=replace(my_string,replacements)

关于ruby - 在使用 Mechanize 进行抓取时,我总是在 Ruby 2.0 中遇到 UndefinedConversionError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18559293/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com