簡體   English   中英

使用具有特定編碼的Jsoup從html字符串中提取文本

[英]Extract text from html string using Jsoup with specific encoding

這是我所擁有的-

String html = "<p><b>Annie's and Lärabar</b></p>"

運行以下命令后-

org.jsoup.nodes.Document doc = Jsoup.parse(html);
Element p= doc.select("p").first();
String s = p.text();
System.out.println(s);

輸出- "Annie's and L?rabar".

字符“ä”成為問號。

我的JVM環境是“ iso-8859-1”,在我看來Jsoup的默認編碼是utf-8。 我想強制Jsoup.parse()在解析html字符串時使用“ iso-8859-1”。

我閱讀了API和googleed示例,但是我找不到一個表明Jsoup.parse()實際上可以在解析字符串時采用特定編碼的示例?

有人可以幫忙嗎? 先感謝您!

-Cyn

您可以將char設置為Document,如下所示

org.jsoup.nodes.Document doc = Jsoup.parse(html);
doc.charset(Charset charset);
Element p= doc.select("p").first();
String s = p.text();

希望能有所幫助。 請參閱: https : //jsoup.org/apidocs/org/jsoup/nodes/Document.html#charset-java.nio.charset.Charset-

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM