Java访问指定URL并获取网页源代码

如题 Java访问指定URL并获取网页源代码

1.编写useSourceViewer 类的基本框架,该类仅包括无返回值的main ()方法,该方法从参数中获取URL,通过输入缓冲和输出缓冲将该URL 原码输出。
2.编写useSourceViewer 类,代码如下:
import java.net.*;
import java.io.*;
public class useSourceViewer
{
public static void main (String[] args)
{
if (args.length > 0)
{
try
{
//读入URL
URL u = new URL(args[0]);
InputStream in = u.openStream( );
// 为增加性能存储输入流
in = new BufferedInputStream(in);
// 将输入流连接到阅读器
Reader r = new InputStreamReader(in);
int c;
while ((c = r.read( )) != -1)
{
System.out.print((char) c);
}
Object o = u.getContent( );
System.out.println("I got a " + o.getClass().getName( ));
}
catch (MalformedURLException e)
{
System.err.println(args[0] + " is not a parseable URL");
}
catch (IOException e)
{
System.err.println(e);
}
} // end if
} // end main
} // end SourceViewer}追问

从哪复制的我操,,你自己都看不懂吧

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-07-02
JS好像可以做。。。java不了解追问

js 底层无非也就是Java代码 ,,

追答

额。。。。。

相似回答