我正在使用 Boilerpipe使用此代码从 url 中提取文本:
URL url = new URL("http://www.example.com/some-location/index.html");
String text = ArticleExtractor.INSTANCE.getText(url);
字符串 text
只包含 html 页面的文本,但我需要从中提取整个 html 代码。
有没有人用过这个库并且知道如何提取 HTML 代码?
您可以查看 demo page有关图书馆的更多信息。
最佳答案
对于像这样简单的事情,你真的不需要外部库:
URL url = new URL("http://www.google.com");
InputStream is = (InputStream) url.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
StringBuffer sb = new StringBuffer();
while((line = br.readLine()) != null){
sb.append(line);
}
String htmlContent = sb.toString();
关于java - 从 URL 中提取 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5213558/