gpt4 book ai didi

java - 如何使用JAVA inputStream从HTML代码中抓取汉字?

转载 作者:行者123 更新时间:2023-11-30 07:05:22 25 4
gpt4 key购买 nike

我想使用以下方法从网站下载一些数据。

下载英文/数字内容没有问题,但当我尝试抓取中文内容时,它不会生成正确的中文字符。

String url = "https://hk.finance.yahoo.com/q/ct?s=1928.HK";         
URL yahooUrl = new URL(url);
reader = new BufferedReader(new InputStreamReader(yahooUrl.openStream()));
String line ="";
while((line =reader.readLine()) != null){
htmlData.append(line);
}
Pattern p = Pattern.compile(
Pattern.quote("<div class=\"title\"><h2>")+ "(.*?)"
+Pattern.quote("</h2>"));
Matcher match = p.matcher(htmlData.toString());
if(match.find()){
stockName = match.group(1);
}

有人知道如何使用 Java 输入流从互联网获取其他语言的内容吗?

最佳答案

在您的情况下,您没有为 InputStreamReader 指定字符编码,因此接受平台的默认字符集。要读取中文字符,请使用 UTF-8 字符集:

reader = new BufferedReader(new InputStreamReader(yahooUrl.openStream(), "UTF-8"));

关于java - 如何使用JAVA inputStream从HTML代码中抓取汉字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40237238/

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