[英]how can i get spesific words from an url in java
我怎样才能从 java 中的 url 中获得特定的词。 就像我想从调用 blablabla 的 class 获取数据。 这是我的代码。
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);
}
看看Jsoup ,这将允许您获取 web 页面的内容,而不是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) {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.