使用jsoup从网址获取内容

时间:2016-10-25 09:29:34

标签: java url jsoup

我想得到url(即)的全部内容,直到该url的结尾。 但由于部分加载而得到的内容很少..无法获得剩余的内容......即使在部分加载后,有什么办法可以从网址获取整个内容......

         String url = "URL/"; // getting URL
          try {
         Document doc = Jsoup.connect(url).get();
         FileWriter fw=null;
         BufferedWriter bw=null;
        fw=new FileWriter("D:\\url.txt");
        bw=new BufferedWriter(fw);
        String line = doc.text();
         System.out.println(line);
            bw.write(line);
        } catch (IOException ex) {
       Logger.getLogger(NewJFrame.class.getName())
       .log(Level.SEVERE, null,  ex);
    }

2 个答案:

答案 0 :(得分:0)

对不起,迟到的回答,晚餐......

答案可以在这里找到:Read url to string in few lines of java code

如果您的问题是重复的问题,请确认...

无论如何,传统的做法是这样的:

URL website = new URL("example.com");
        URLConnection connection = website.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                    connection.getInputStream()));

        StringBuilder response = new StringBuilder();
        String inputLine;

        while ((inputLine = in.readLine()) != null) 
            response.append(inputLine);

        in.close();

希望我帮忙!

答案 1 :(得分:0)

    URL website;
    try {
        website = new URL("https://news.google.co.in/");

    URLConnection connection = website.openConnection();
    BufferedReader in = new BufferedReader(
                            new InputStreamReader(
                                connection.getInputStream()));

    StringBuilder response = new StringBuilder();
    String inputLine;

    while ((inputLine = in.readLine()) != null) 
        response.append(inputLine);

    in.close();
        System.out.print(response.toString());
  } catch (MalformedURLException ex) {
                                                                                    Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
    Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
    }

获取html输出