资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

用java读网站源代码 用java读网站源代码的软件

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

1.编写useSourceViewer 类的基本框架,该类仅包括无返回值的main ()方法,该方法从参数中获取URL,通过输入缓冲和输出缓冲将该URL 原码输出。

创新互联公司是一家专业提供沭阳企业网站建设,专注与做网站、网站建设、H5场景定制、小程序制作等业务。10年已为沭阳众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。

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}

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中如何根据一个网址获得该网页的源代码,急求

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

public class HttpTest {

String urlString;

public static void main(String[] args) throws Exception {

HttpTest client = new HttpTest(网址);

client.run();

}

public HttpTest(String urlString) {

this.urlString = urlString;

}

public void run() throws Exception {

//生成一个URL对象

URL url = new URL(urlString);

//打开URL

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

//得到输入流,即获得了网页的内容

BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection

.getInputStream()));

String line;

// 读取输入流的数据,并显示

while ((line = reader.readLine()) != null){

System.out.println(line);

}

}

}

怎样使用java读取网页源码 How To Get URL Content In Java

使用java读取网页源码 How To Get URL Content In Java

public class GetURLContent {

public static void main(String[] args) {

URL url;

try {

// get URL content

url = new URL("");

URLConnection conn = url.openConnection();

// open the stream and put it into BufferedReader

BufferedReader br = new BufferedReader(

new InputStreamReader(conn.getInputStream()));

String inputLine;

//save to this filename

String fileName = "/users/mkyong/test.html";

File file = new File(fileName);

if (!file.exists()) {

file.createNewFile();

}

//use FileWriter to write file

FileWriter fw = new FileWriter(file.getAbsoluteFile());

BufferedWriter bw = new BufferedWriter(fw);

while ((inputLine = br.readLine()) != null) {

bw.write(inputLine);

}

bw.close();

br.close();

System.out.println("Done");

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}


新闻标题:用java读网站源代码 用java读网站源代码的软件
路径分享:http://www.cdkjz.cn/article/doheogh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220