简体   繁体   English

没有http-equiv元标记且字符集的HTML文件可能不是UTF-8

[英]HTML files with no http-equiv meta tag and the charset may be other than UTF-8

we are using jsoup - excellent thanks. 我们正在使用jsoup-非常感谢。

We may get HTML files with no http-equiv meta tag and the charset may be other than UTF-8. 我们可能会获得没有http-equiv元标记的HTML文件,并且字符集可能不是UTF-8。 How is it best to handle this please. 请问如何最好地处理这个问题。 We can have a list of encodings and try them but I am not sure how to tell programatically if something is wrong. 我们可以列出一些编码,然后尝试使用它们,但是我不确定如何以编程方式告诉您是否出了问题。 Would jsoup throw an IOException? jsoup会抛出IOException吗?

Jsoup will try to determine the encoding by the content type header or http equiv tag , if you have none of them it will use utf8 . Jsoup将尝试通过内容类型标头http equiv标签确定编码,如果您都不使用utf8 ,则将使用utf8 Not sure if jsoup can do more for you here. 不知道jsoup是否可以在这里为您做更多的事情。

But you can try another approach: 但是您可以尝试另一种方法:

Implement a class that reads the files for you. 实现一个为您读取文件的类。 There you can take care of all encoding issues. 在那里您可以解决所有编码问题。 As a result such a class should give you proper encoded string or at least the encoding that's used for your input. 结果,这样的类应该为您提供正确的编码字符串,或者至少为您的输入使用编码。

(html input) --> [encoding class] --normalized encoding--> [jsoup] --> (whatever)   

Jsoup can now parse that input with a known encoding. 现在,Jsoup可以使用已知的编码来解析该输入。

I guess changes on the html-creation thing is not possible, isn't it? 我猜不可能对html创建内容进行更改,不是吗?

Some further readings: 一些进一步的阅读:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM