您的位置:首页 > 理论基础 > 计算机网络

Spider网络爬虫学习——通过Get、Post方法直接抓取网页内容

2014-05-23 15:39 537 查看
网络爬虫(又被称为网页蜘蛛,网络机器人),是一种按照一定的规则,自动的抓取万维网信息的程序或者脚本。另外一些不常使用的名字还有蚂蚁,自动索引,模拟程序或者蠕虫。我们常用网络爬虫程序,在网络上抓取自己感兴趣的信息,那么网络爬虫程序究竟是怎么写的呢?接下来将一步步展开介绍。

今天,首先介绍下怎么抓取网页上的数据。在任何一个网页上,我们可以右键查看网页源代码,看到网页上的各元素。实际上,我们每次向服务器发起请求的时候,服务器正是返回给我们这些数据,而浏览器的功能则是解析这些数据,展现给我们看。爬虫程序正是把这些个数据Down下来,然后通过自己指定的规则,筛选出有用的信息。那么,我们要如何获取这些数据呢?

首先,介绍下Http协议的两种请求方法。Http协议共有Get和Post两种请求方法,最主要的区别也是最明显的区别就是:Get请求是把参数带在URL中;而Post请求则是把参数放在Header后面的实体中。所以,我们可以分别模拟Get和Post两种方法来获取网页内容。

代码如下:package com.smallstone.spider;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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;

/**
* Get、Post方法抓取网页内容
*
* @author SmallStone
* @date 2014-5-23
*/
public class Spider {
// http客户端实例
public static HttpClient httpClient = new HttpClient();

/**
* 通过Get请求方法抓取网页内容
*
* @param url
* @return
* @throws HttpException
* @throws IOException
*/
public static Boolean downloadPageByGet(String url) throws HttpException, IOException {
// 返回状态码
int statusCode;
// 文件名
String fileName = "getExample.txt";
// 输入流
InputStream inputStream = null;
// 输出流
OutputStream outputStream = null;
// http选择Get方法
GetMethod getMethod = new GetMethod(url);

// 执行方法
statusCode = httpClient.executeMethod(getMethod);

// 判断返回状态码,OK则为成功,返回true;否则为失败,返回false
if(statusCode == HttpStatus.SC_OK) {
inputStream = getMethod.getResponseBodyAsStream();
outputStream = new FileOutputStream(fileName);

// 输出到文件
int readByte = -1;
while( (readByte = inputStream.read()) > 0) {
outputStream.write(readByte);
}

// 关闭输入、输出流,释放资源
if(inputStream != null)
inputStream.close();
if(outputStream != null)
outputStream.close();

return true;
}
else
return false;
}

/**
* 通过Post请求方法抓取网页内容
*
* @param url
* @return
* @throws HttpException
* @throws IOException
*/
public static Boolean downloadPageByPost(String url) throws HttpException, IOException {
// 返回状态码
int statusCode;
// 文件名
String fileName = "postExample.txt";
// 输入流
InputStream inputStream = null;
// 输出流
OutputStream outputStream = null;
// http选择post方法
PostMethod postMethod = new PostMethod(url);
// post方法的参数
NameValuePair[] postData = new NameValuePair[2];

// 设置post方法参数
postData[0] = new NameValuePair("userName", "xxxxxx");
postData[1] = new NameValuePair("password", "xxxxxx");
postMethod.addParameters(postData);

// 执行方法
statusCode = httpClient.executeMethod(postMethod);

// 判断返回状态码,OK则为成功,返回true;否则为失败,返回false
if(statusCode == HttpStatus.SC_OK) {
inputStream = postMethod.getResponseBodyAsStream();
outputStream = new FileOutputStream(fileName);

// 输出到文件
int readByte = -1;
while((readByte = inputStream.read()) > 0) {
outputStream.write(readByte);
}

// 关闭输入、输出流,释放资源
if(inputStream != null)
inputStream.close();
if(outputStream != null)
outputStream.close();

return true;
}
else
return false;
}

/**
* 主方法
*
* @param args
* @throws HttpException
* @throws IOException
*/
public static void main(String[] args) throws HttpException, IOException {
// Get请求URL
String getURL = "http://www.baidu.com"; // 百度首页
// Post请求URL
String postURL = "http://xxxxxxxxxxx"; // 自己找一个post的网站

// 通过Get请求方法抓取网页内容
downloadPageByGet(getURL);
// 通过Post请求方法抓取网页内容
downloadPageByPost(postURL);
}

}

downloadPageByGet( )和downloadPageByPost( )这两个方法正是模拟了抓取内容的过程。可以发现,Post请求的时候,需要设置参数;而Get请求的时候没有设置,而是直接URL中直接填写(百度首页这个例子没有体现)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐