关闭

使用Java抓取网页数据

发表于:2016-1-11 11:23

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:曾经留下的足迹    来源:51Testing软件测试网采编

  二、使用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 <NameValuePair> nvps = new ArrayList <NameValuePair>();
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();
}
22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号