【问题标题】:Connect and print html page that follow with thread连接并打印跟在线程后面的 html 页面
【发布时间】:2014-11-08 01:03:41
【问题描述】:

大家好,我在使用 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("&quot;", "\"");
                // 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 的位置,我需要告诉连接我有线程,但我不知道如何。 请帮我 谢谢。

【问题讨论】:

  • 你考虑过使用 HtmlUnit 吗?
  • 不知道是什么我会读到的
  • 我不知道为什么我没有得到正确的 html 页面这是我的问题
  • 问题解决了,我只需要添加我有不同字符集的BufferedReader

标签: java html printing connect


【解决方案1】:

问题解决了,我只需要在 BufferedReader 中添加我有不同的字符集

BufferedReader in = new BufferedReader(new InputStreamReader(
                    yc.getInputStream(),"gbk"));

【讨论】:

    猜你喜欢
    • 2015-02-11
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 2016-11-26
    • 2010-12-08
    • 1970-01-01
    相关资源
    最近更新 更多