作为一个屌丝程序员不得不收藏的工具类 一 网站爬虫工具类

本文介绍了一个Java程序如何通过网络从指定URL抓取网页内容的方法,并提供了执行效率测试的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;


public class URLFile {
	/**
	 *  根据URL从网页上获取资料
	 * @param urlPath 链接地址http://开头
	 * @return
	 */
	public static String getResponseDataFromWeb(String urlPath) {
		StringBuffer buffer = new StringBuffer();
		HttpURLConnection httpUrlConn = null;
		HttpURLConnection isexit = null;
		InputStream inputStream = null;
		BufferedReader bufferedReader = null;
		InputStreamReader inputStreamReader = null;
		int state = 0;
		try {
			URL u = new URL(urlPath);
			isexit = (HttpURLConnection) u.openConnection();
			state = isexit.getResponseCode();
			isexit.disconnect();
			if (state == 404) {
				return "404";
			}
			URL url = new URL(urlPath);
			httpUrlConn = (HttpURLConnection) url.openConnection();
			httpUrlConn.setDoOutput(false);
			httpUrlConn.setDoInput(true);
			httpUrlConn.setUseCaches(false);
			httpUrlConn.setRequestMethod("GET");
			httpUrlConn.connect();
			// 将返回的输入流转换成字符串
			inputStream = httpUrlConn.getInputStream();
			inputStreamReader = new InputStreamReader(inputStream, "utf-8");
			bufferedReader = new BufferedReader(inputStreamReader);

			String str = null;
			while ((str = bufferedReader.readLine()) != null) {
				buffer.append(str);
			}

		} catch (Exception e) {
			e.printStackTrace();
			return "404";
		} finally {
			try {
				if (state != 404) {
					bufferedReader.close();
					bufferedReader = null;
					inputStreamReader.close();
					// 释放资源
					inputStream.close();
					inputStream = null;
					inputStreamReader = null;
					httpUrlConn.disconnect();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return buffer.toString();
	}
	/**
	 * 打开链接判断用时
	 * @param urlPath 链接地址http://开头
	 * @return
	 */
	public static String doTimer(String urlPath) {
		StringBuffer buffer = new StringBuffer();
		HttpURLConnection httpUrlConn = null;
		int state = 0;
		long useTime=0;
		try {
			URL url = new URL(urlPath);
			Date d1=new Date();
			httpUrlConn = (HttpURLConnection) url.openConnection();
			httpUrlConn.setDoOutput(false);
			httpUrlConn.setDoInput(true);
			httpUrlConn.setUseCaches(false);
			httpUrlConn.setRequestMethod("GET");
			state = httpUrlConn.getResponseCode();
			if (state == 404) {
				buffer.append("链接错误404:"+urlPath);
				return buffer.toString();
			}
			httpUrlConn.connect();
			Date d2=new Date();
			useTime=d2.getTime()-d1.getTime();
			buffer.append("任务完成:"+urlPath+",用时:"+useTime/1000+"秒");
		} catch (Exception e) {
			e.printStackTrace();
			buffer.append("系统异常:"+e.getMessage());
			return buffer.toString();
		} finally {
			try {
				if (state != 404) {
					httpUrlConn.disconnect();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return buffer.toString();
	}
	
	public static void main(String[] args) {
		String url="http://www.baidu.com";
		URLFile uf=new URLFile();
		System.out.println(uf.doTimer(url));
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值