大家好,我在使用 java 获取完整的 html 文件时遇到问题。 我正在使用这个功能:
public static void secondUrl() {
String expr = "<div//s+class=\"t_fsz\"[^>]*>" + "(.*)?"
+ "\r\n*</div>*";
try {
URL google = new URL(
"http://www.kr16.com/thread-90107-1-1.html");
HttpURLConnection yc = (HttpURLConnection) google.openConnection();
yc.setInstanceFollowRedirects(true); //you still need to handle redirect manully.
HttpURLConnection.setFollowRedirects(true);
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine = "";
Pattern patt = Pattern.compile("<div//s+class=\"t_fsz\">",
Pattern.DOTALL | Pattern.UNIX_LINES);
int counter = 1;
while ((inputLine = in.readLine()) != null) {
System.out.println(counter++ + inputLine);
// Matcher m = patt.matcher(inputLine);
// while (m.find()) {
//
// String extractedText = m.group();
//
// // extractedText = extractedText.replaceAll("<.*?>", "");
// // extractedText = extractedText.replaceAll(""", "\"");
// System.out.println(counter++ + ". " + extractedText);
// System.out.println();
//
// }
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
不要查看正则表达式。我正在尝试连接“http://www.kr16.com/thread-90107-1-1.html” 当我打印源页面时没有成功,我得到了错误的页面。找不到任何解决方案。 我知道问题出在 thread-90107-1-1.html 的位置,我需要告诉连接我有线程,但我不知道如何。 请帮我 谢谢你。
问题解决了,我只需要在 BufferedReader 中添加我有不同的字符集
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream(),"gbk"));