我如何从Java的URL中获得特殊的词

问题描述 投票:1回答:1

如何从Java中的URL获取特殊单词。就像我想从类中调用blablabla这样的数据。这是我的代码。

    URL url = new URL("https://www.doviz.com/");
    URLConnection connect = url.openConnection();
    InputStream is = connect.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String line = null;
    while((line = br.readLine()) != null)
    {
        System.out.println(line);
    }
java url
1个回答
1
投票

看一下Jsoup,这将使您获得网页的内容和NOT HTML代码。假设它将扮演浏览器的角色,它将HTML标记解析为人类可读的文本。

一旦获得了字符串形式的页面内容,就可以使用任何出现次数计数算法来计算单词的出现次数。

使用它的简单示例:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
/*   ........  */
String URL = "https://www.doviz.com/";       
Document doc = Jsoup.connect(URL).get();
String text = doc.body().text();
System.out.println(text);

编辑

如果您不想使用解析器(如您在注释中提到的那样,您不需要外部库),您将获得页面的整个HTML代码,这就是您可以使用的方式

try {
    URL url = new URL("https://www.doviz.com/");       

    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str;
    while ((str = in.readLine()) != null) {
        str = in.readLine().toString();
        System.out.println(str);
        /*str will get each time the new line, if you want to store the whole text in str 
           you can use concatenation (str+ = in.readLine().toString())*/
    }
    in.close();
} catch (Exception e) {} 
© www.soinside.com 2019 - 2024. All rights reserved.