抓取首页源代码java 抓取网页源代码

JAVA WEB怎样抓取一个HTML源代码import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
public class Test {
public static void main(String argv[]) {
//Test t = new Test();
//t.first();
//Test.TSR();
//Test.testDouPrase();
try {
Test.testNetStream();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testNetStream() throws Exception{
URL url = null;
url = new URL("");
InputStream in = url.openStream();
byte[] b = new byte[100000];
InputStream ins = url.openStream();
ins.read(b);
ins.close();
String s = new String(b);
System.out.println(s);
}
}
java中如何根据一个网址获得该网页的源代码?package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpTest {
private String u;
private String encoding;
public static void main(String[] args) throws Exception {
HttpTest client = new HttpTest("", "UTF-8");
client.run();
}
public HttpTest(String u, String encoding) {
this.u = u;
this.encoding = encoding;
}
public void run() throws Exception {
URL url = new URL(u);// 根据链接(字符串格式),生成一个URL对象
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();// 打开URL
BufferedReader reader = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream(), encoding));// 得到输入流,即获得了网页的内容
String line; // 读取输入流的数据,并显示
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}
根据具体问题类型,进行步骤拆解/原因原理分析/内容拓展等 。
具体步骤如下:/导致这种情况的原因主要是……
Java访问指定URL并获取网页源代码1.编写useSourceViewer 类抓取首页源代码java的基本框架 , 该类仅包括无返回值抓取首页源代码java的main ()方法 , 该方法从参数中获取URL,通过输入缓冲和输出缓冲将该URL 原码输出 。
2.编写useSourceViewer 类,代码如下抓取首页源代码java:
import java.net.*;
import java.io.*;
public class useSourceViewer
{
public static void main (String[] args)
{
if (args.length0)
{
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}
【抓取首页源代码java 抓取网页源代码】抓取首页源代码java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于抓取网页源代码、抓取首页源代码java的信息别忘了在本站进行查找喔 。

    推荐阅读