Ending、网络爬虫-HttpClient系列
2017-06-14 16:51
197 查看
HttpClient(我也是不太理解的)
提问一下
先是说下自己跟着学的《自己动手写网络爬虫》,不过一直运行出错,感觉可能是post方式不太对,不过好像换了get也是同样的错误,以下代码希望大神帮忙解答。package cn.sitron.worm.text;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
/**
* java网页抓取示例
* @author Ending、
* 备注:(引用commons-httpclient.jar)(commons-logging.jar)(commons-codec.jar)
*/
public class RetrivePage {
private static HttpClient httpClient=new HttpClient();
//设置代理服务器
static{
//设置代理服务器的IP地址和端口
httpClient.getHostConfiguration().setProxy("118.144.176.5", 3128);
}
public static boolean downloadPage(String path) throws HttpException,IOException{
InputStream input=null;
OutputStream output=null;
//得到post方法
// PostMethod postMethod=new PostMethod(path);
GetMethod getMethod=new GetMethod(path);
//设置post方法的参数
// NameValuePair[] postData=new NameValuePair[2];
// postData[0] =new NameValuePair("name","lietu");
// postData[1] =new NameValuePair("password","*****");
// postMethod.addParameters(postData);
//执行,返回状态码
int statusCode = httpClient.executeMethod(getMethod);
//针对状态码进行处理(简单起见,只处理返回值为200的状态码)
if(statusCode==HttpStatus.SC_OK){
input=getMethod.getResponseBodyAsStream();
//得到文件名
String fileName= path.substring(path.lastIndexOf('/')+1);
//获得文件输出流
output=new FileOutputStream(fileName);
//输出到文件
int tempByte=1;
while((tempByte=input.read())>0){
System.out.println("SUCCESS");
output.write(tempByte);
}
//关闭输入输出流
if(input!=null){
input.close();
}
if(output!=null){
output.close();
}
return true;
}
return false;
}
/**
* 测试代码
*/
public static void main(String[] args) {
//抓取lietu首页,输出
try {
RetrivePage.downloadPage("http://www.baidu.com/");
} catch (Exception e) {
// TODO: handle exception
}
}
}
案例环节
这是感觉也是看httpClient手册敲的,不过在post方式上依旧有同样的问题,post方式使用了代理公布器(以下下载方式:下载链接),就为了获取代理IP地址和端口,然而我等了10分钟只给我一条可用的数据接下来我用了新的方式抓取网页,以下代码
package cn.sitron.worm.text;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
/**
* httpclient——get、host方法
* @author Ending、
*
*/
public class EasyPage {
//代理服务器如果是HTTPS,就讲页面改为https
private static final String PATH="http://java.sun.com";
public static void main(String[] args) throws IOException{
HttpClient httpClient=new HttpClient();
//使用get方法
HttpMethod method=new GetMethod(PATH);
httpClient.executeMethod(method);
//打印服务器返回状态
System.out.println(method.getStatusLine());
//打印返回信息
System.out.println(method.getResponseBodyAsString());
//释放信息
method.releaseConnection();
System.out.println("----------------------------------");
System.out.println("----------------------------------");
EasyPage.post();
}
/**
* 301报错提示、信息: Redirect requested but followRedirects is disabled
* @throws IOException
*/
public static void post() throws IOException,HttpException{
HttpClient client=new HttpClient();
client.getHostConfiguration().setProxy("118.144.176.5", 3128);
HttpMethod method=new PostMethod(PATH);
client.executeMethod(method);
System.out.println(method.getStatusLine());
int statusCode=client.executeMethod(method);
if(statusCode==301){
System.err.println("信息: Redirect requested but followRedirects is disabled");
System.err.println("信息: 重定向请求,followRedirects拒绝");
}else{
System.out.println(method.getResponseBodyAsString());
}
method.releaseConnection();
}
}对于get运行时没问题的,但是post···还是。
以上是下午上班突然想到的,抓取功能,不是有很多不足,希望大神留言分享经验,有比我还要小白(比我还不懂)的也可以留言,我也可以出些基础的文档。
相关文章推荐
- 数学之美系列之六:图论和网络爬虫
- 基于HttpClient实现网络爬虫~以百度新闻为例
- python网络爬虫系列教程——python网络数据爬虫误区,让你的爬虫更像人类
- 数学之美系列之六:图论和网络爬虫
- 网络相关系列之中的一个:Android中使用HttpClient发送HTTP请求
- 基于HttpClient实现网络爬虫~以百度新闻为例
- 使用htmlparser和httpClient制作网络爬虫——JavaScript隐藏邮箱解析问题
- 用HttpClient实现网络爬虫
- 网络相关系列之一:Android中使用HttpClient发送HTTP请求
- Java爬虫系列之一HttpClient【爬取京东Python书籍信息】
- 使用HttpClient做的一个简单的网络爬虫
- 使用HttpClient和HtmlParser实现网络爬虫
- 网络爬虫框架的搭建以及使用HttpClient抓取解析网页的详细步骤
- 使用HttpClient实现网络爬虫 1
- [置顶] 【matlab 爬虫】用matlab做网络爬虫入门系列1
- Python爬虫入门实战系列(一)--爬取网络小说并存放至txt文件
- 网络爬虫入门(二)模拟提交以及HttpClient修正
- HttpClient实现简单的网络爬虫功能
- 分布式网络爬虫关键技术分析与实现系列
- 基于HttpClient实现网络爬虫~以百度新闻为例