我终于设法解析了网站的部分内容:
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"> </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/