使用apache的HttpClient实现网页抓取功能
/**
* 根据url抓取字符串 返回字符串
*
* @param urlstr
* String
* @return String
*/
public static String snatch(String urlstr, String encode) {
String rs = "";
// Create an instance of HttpClient.
HttpClient client = new HttpClient();
// Create a method instance.
GetMethod method = new GetMethod(urlstr);
// Provide custom retry handler is necessary
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(0, false));
// method.getParams().setParameter(HttpMethodParams.USER_AGENT,
// "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
try {
// Execute the method.
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + statusCode);
System.err.println("Method failed: " + method.getStatusLine());
}else{
// Read the response body.
byte[] responseBody = method.getResponseBody();
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary
// data
if (!method.getResponseCharSet().trim().equalsIgnoreCase(
"ISO-8859-1")) {
rs = new String(responseBody, method.getResponseCharSet());
} else {
if (encode != null && encode.length() > 0)
rs = new String(responseBody, encode);
else
rs = new String(responseBody, "gb2312");
}
}
} catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
System.err.println("=============" + urlstr);
e.printStackTrace();
}catch(java.lang.IllegalArgumentException e){
System.err.println("报错的url是:"+urlstr);
e.printStackTrace();
} finally {
// Release the connection.
method.releaseConnection();
}
return rs;
}
以上代码就是一个简单的HttpClient远程抓取页面源码了,不过记得要
import org.apache.commons.httpclient.*;
还有就是中间注释掉的代码
// method.getParams().setParameter(HttpMethodParams.USER_AGENT,
// "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
大家要注意点,因为有些网站是设置的是防止爬虫形式抓取的,所以如果有些时候你抓取不到的时候你需要加上这段代码了!
直接调用snatch("url地址一定要加http://",“编码”)方法
分享到:
相关推荐
apache httpclient jar包 httpcomponents-client-4.2.5
apache httpclient document apache httpcore document
apache HttpClient jar包
目录前言正文OkHttp使用示例源码解析Apache HttpClient使用示例源码解析GoogleHttpClient哪个更香?总结声明 前言 前八篇文章介绍完了feign-core核心内容,从本篇开始将介绍它的“其它模块”。其实核心模块可以独立...
Apache httpclient 4.5.12版本源码,不包含jar包。适合学习原理,查看各种接口的实现。
org.apache httpclient 4.5源代码,通过eclipse代码项目代码后可以直接查看httpclient源代码,与您的工程代码关连起来。
wechatpay-apache-httpclient-0.2.1.jar
apache httpclient 源码和 jar包 源码不是反编译的
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods....
NULL 博文链接:https://zywang.iteye.com/blog/916489
本实例描述了Android平台下使用Apache开源项目的HttpClient访问网络的示例。分别使用HttpClient下的两个子实现类,DefaultHttpClient和AndroidHttpClient分别进行演示。
目的是为了替换难用的Apache HttpClient。easy-okhttp是对OkHttp网络框架封装,提供文件上传和下载,表单(含文件)提交,链式调用,支持HTTPS和自定义签名证书等特性。 OkHttp网络框架的流行始于Android,但是在Java...
如果你是使用Apache HttpClient的商户开发者,可以使用它构造HttpClient。得到的HttpClient在执行请求时将自动携带身份认证信息,并检查应答的微信支付签名。 项目状态 当前版本0.2.2为测试版本。请商户的专业技术...
详细讲解Apache httpclient的原理以及使用 深入理解httpclient
Apache_HttpClient4.x简明教程高清PDF版.pdf Apache_HttpClient4.x简明教程高清PDF版.pdf
Apache HttpComponents Client的4.5.6版本 ,下载引入工程即可。
使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。 双语对照,边学技术、边学英语。
赠送jar包:httpclient-4.5.13.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。
NULL 博文链接:https://antlove.iteye.com/blog/1815470