gpt4 book ai didi

html - Ruby、Nokogiri : how do i ensure UTF8 throughout nokogiri parsing, erb 模板和编码 HTML 文件

转载 作者:太空狗 更新时间:2023-10-29 14:48:59 26 4
gpt4 key购买 nike

我终于设法解析了网站的部分内容:

get '/' do
url = '<website>'
data = Nokogiri::HTML(open(url))
@rows = data.css("td[valign=top] table tr")
erb :muster
end

现在我试图在我的 View 中提取某行。因此我输入了我的 HTML 代码:

<%= @rows[2] %> 

它实际上返回了代码,但它在 UTF8 方面有问题:

<td class="class_name">&nbsp;</td>

相反它说

<td class="class_name">�</td>

如何在 nokogiri 解析、erb 和 HTML 生成期间确保使用 UTF8?

最佳答案

参见:http://www.nokogiri.org/tutorials/parsing_an_html_xml_document.html#encoding

在您的情况下,文档声明它是使用 iso8859 编码的:

<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">

您可以执行以下操作以强制 Nokogiri 将流视为 UTF-8:

data = Nokogiri::HTML(open(url), nil, Encoding::UTF_8.to_s)

关于html - Ruby、Nokogiri : how do i ensure UTF8 throughout nokogiri parsing, erb 模板和编码 HTML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28253912/

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