读取 html 页面并将其 HTML 代码保存在文本文件中的 Java 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13176405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Program to read a html page and save its HTML code in a text file
提问by Selva Perumal
I am developing a project in that i have a module in which i need to write a Java Program to read a html page and save its HTML code of the page in a text file. Please can anyone give that above said program .......
我正在开发一个项目,因为我有一个模块,我需要在其中编写一个 Java 程序来读取 html 页面并将页面的 HTML 代码保存在文本文件中。请任何人都可以给出上述程序......
回答by Sander
have a look at http://docs.oracle.com/javase/tutorial/networking/urls/readingURL.htmlyou dont even need an external library. Combine this with a Bufferedwriter:
看看http://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html你甚至不需要外部库。将其与 Bufferedwriter 结合使用:
import java.net.*;
import java.io.*;
import java.util.*;
public class URLReader {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.oracle.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
BufferedWriter writer = new BufferedWriter(new FileWriter("outputfile.txt"));
String inputLine;
while ((inputLine = in.readLine()) != null){
try{
writer.write(inputLine);
}
catch(IOException e){
e.printStackTrace();
return;
}
}
in.close();
writer.close();
}
}
回答by Ankit
You can do this task using HTMLParser, Read about it here
您可以使用 HTMLParser 完成此任务,请在此处阅读