How to tell Nokogiri when parsing a document not to convert it a different encoding (in my case not to convert &paund; to to anything else)
How do I tell Nokogiri not to convert a document to a different encoding, in my case not to convert &paund;
to to anything else?
I have a file containing:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<span>£</span>
</body>
</html>
I parse it with Nokogiri:
d = Nokogiri::HTML.parse(open('/tmp/in.html', 'r'))
If I print document "d
" I get:
<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n
<html>\n
<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"></head>\n
<body>\n
<span>\302\243</span>\n
</body>\n
</html>\n
Note: £
became "\302\243" (or £
that was encoded in ISO开发者_StackOverflow中文版-8859-1 became encoded in UTF-8)
If I save document "d" to a file:
open('/tmp/out.html', 'w') do |out|
out << d.to_html
end
I get the following:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
<body>
<span>ВЈ</span>
</body>
</html>
After parsing the document with &paund;
, and saving it to a file, I got two symbols instead "BJ
".
I think I am not specifying encoding at some step, but I am not sure where.
Definition of 'parse' from Nokogiri from documentation, look for encoding:
# File lib/nokogiri/html.rb, line 22
22: def parse thing, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_HTML, &block
23: Document.parse(thing, url, encoding, options, &block)
24: end
精彩评论