gpt4 book ai didi

java - 如何使用 Java 从网页中读取文本?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:08:54 24 4
gpt4 key购买 nike

我想从网页中读取文本。我不想获取网页的 HTML 代码。我找到这段代码:

    try {
// Create a URL for the desired page
URL url = new URL("http://www.uefa.com/uefa/aboutuefa/organisation/congress/news/newsid=1772321.html#uefa+moving+with+tide+history");

// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
str = in.readLine().toString();
System.out.println(str);
// str is one line of text; readLine() strips the newline character(s)
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}

但是这段代码给了我网页的 HTML 代码。我想在这个页面中获取整个文本。我如何使用 Java 执行此操作?

最佳答案

您可能想看看 jsoup为此:

String html = "<p>An <a href='http://example.com/'><b>example</b></a> link.</p>";
Document doc = Jsoup.parse(html);
String text = doc.body().text(); // "An example link"

这个例子摘自他们网站上的一个例子。

关于java - 如何使用 Java 从网页中读取文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9825798/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com