java采集网页数据_使用JAVA抓取网页数据

本文介绍了如何使用Java的HttpClient库通过HttpGet和HttpPost两种方式抓取和解析网页数据。首先展示了HttpGet方法,包括处理gzip压缩的响应。然后详细解释了HttpPost方式,设置请求头并传递表单参数。最后提供了读取和处理HTTP响应内容的方法。
摘要由CSDN通过智能技术生成

一、使用 HttpClient 抓取网页数据

public String getHtml(String htmlurl) throws IOException {

StringBuffer sb = new StringBuffer();

String acceptEncoding = "";

/* 1.生成 HttpClinet 对象并设置参数 */

HttpClient httpClient = new HttpClient();

GetMethod method = new GetMethod(htmlurl);

int statusCode;

try {

statusCode = httpClient.executeMethod(method);

// 判断访问的状态码

if (statusCode != HttpStatus.SC_OK) {

return null;

} else {

if (method.getResponseHeader("Content-Encoding") != null)

acceptEncoding = method.getResponseHeader(

"Content-Encoding").getValue();

if (acceptEncoding.toLowerCase().indexOf("gzip") > -1) {

// 建立gzip解压工作流

InputStream is;

is = method.getResponseBodyAsStream();

GZIPInputStream gzin = new GZIPInputStream(is);

InputStreamReader isr = new InputStreamReader(gzin, Charset.forName(CHARSET)); // 设置读取流的编码格式,自定义编码

java.io.BufferedReader br = new java.io.BufferedReader(isr);

String tempbf;

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

if(StringUtils.isNotBlank(tempbf)){

sb.append(tempbf);

}

}

isr.close();

gzin.close();

System.out.println(sb);

} else {

InputStreamReader isr;

isr = new InputStreamReader(

method.getResponseBodyAsStream(), CHARSET);

java.io.BufferedReader br = new java.io.BufferedReader(isr);

String tempbf;

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

if(StringUtils.isNotBlank(tempbf)){

sb.append(tempbf);

}

}

isr.close();

}

}

} catch (HttpException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

method.abort();

method.releaseConnection();

return sb.toString();

}

二、使用HttpPost抓取网页数据

private static CloseableHttpClient httpClient;

private static BasicHttpContext httpContext;

private static BasicCookieStore cookieStore;

private static PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

private static RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH).build();

private static RequestConfig localConfig = RequestConfig.copy(globalConfig).setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();

public String getHtml(String url){

HttpClientBuilder builder = HttpClients.custom();

cookieStore = new BasicCookieStore();

builder.setConnectionManager(cm);

builder.setDefaultCookieStore(cookieStore);

builder.setDefaultRequestConfig(globalConfig);

httpClient = builder.build();

httpContext = new BasicHttpContext();

httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

HttpPost httpPost = new HttpPost(url);

httpPost.setConfig(localConfig);

httpPost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

httpPost.setHeader("Accept-Encoding","gzip, deflate");

httpPost.setHeader("Accept-Language","zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");

httpPost.setHeader("Connection","keep-alive");

httpPost.setHeader("Cookie","ASP.NET_SessionId=11vrr4ucwsgeqtmpyfx4hmvx; _5t_trace_sid=89c4ffb8633d267e4ae322a157b52471; _5t_trace_tms=1; CheckCode=X0P64");

httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0");

List nvps = new ArrayList ();

nvps.add(new BasicNameValuePair("pid", "99-C3-57-35-6D-70-3D-F2"));

nvps.add(new BasicNameValuePair("CurrentlyPageIndex", "2"));

httpPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));

try {

CloseableHttpResponse response = httpClient.execute(httpPost,httpContext);

int status = response.getStatusLine().getStatusCode();

if (status >= 200 && status < 300) {

HttpEntity httpEntity = response.getEntity();

if(httpEntity!=null){

String cont = trimLineToString(httpEntity, "UTF-8");

EntityUtils.consume(httpEntity);

return cont;

}

}

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

public synchronized static String trimLineToString(HttpEntity entiry,String charset) {

StringBuffer sb = new StringBuffer();

BufferedReader reader = null;

try {

InputStream instream = entiry.getContent();

reader = new BufferedReader(new InputStreamReader(instream, charset));

String str = null;

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

if(StringUtils.isNotBlank(str)) {

sb.append(str.trim());

}

}

instream.close();

} catch (IllegalStateException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (reader != null) {

try {

reader.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return sb.toString();

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值