【发布时间】:2012-04-07 05:19:38
【问题描述】:
我想从网页中读取文本。我不想获取网页的 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 做到这一点?
【问题讨论】:
-
只解析 HTML 标签中的文本。从那里你可以找到你想要的信息并从那里提取它。
-
如果您正在寻找 HTML to DOM stackoverflow.com/questions/457684/… 可以帮助您。
-
仅供参考 - 您每次迭代都会调用 in.readLine() 两次,因此您实际上是在跳过每一个奇数行。 (只是想我应该指出这段代码中的错误,因为它是谷歌搜索使用 Java 阅读网页的第一批结果之一。)
标签: java