爬虫技术(2)--抓取网页java代码实现

package creeper.part1.capturepage;
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
//爬虫技术(1)--抓取网页
@SuppressWarnings("unused")
public class capturePage {
	public static void main(String[] args) throws Exception {
		//声明一个HttpClient客户端,相当于打开一个浏览器(4.3以后都是CloseableHttpClient以前的已经过时)
		CloseableHttpClient httpClient=HttpClients.createDefault();
		//创建代理,省略...
		
		try {
			//get方法,相当于打开了一个网页
			String url="http://www.baidu.com";
			HttpGet get=new HttpGet(url);
			System.out.println("---------URI----------");
			System.out.println(get.getURI());
			
			//创建响应处理器处理响应内容
			ResponseHandler<String> handler=new ResponseHandler<String>(){
				@Override
				public String handleResponse(HttpResponse response)
						throws ClientProtocolException, IOException {
					int status=response.getStatusLine().getStatusCode();//获取响应状态码
					//对状态码进行判断处理
					if(status>=200 && status<300 ){
						HttpEntity entity=response.getEntity();//获取响应的数据
						return entity==null?null:EntityUtils.toString(entity);
					}else{
						throw new ClientProtocolException("status:"+status);
					}
				}
			};
			//发送请求,相当于敲个回车
			String responseBody=httpClient.execute(get, handler);
			System.out.println("----------------responseBody-----------------");
			System.out.println(responseBody);
			System.out.println("----------------responseBody-----------------");
		} catch (Exception e) {
			
		}finally{
			httpClient.close();
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值