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

本文介绍了网络爬虫的基本原理和如何利用Http协议的Get、Post方法抓取网页数据。Get请求将参数置于URL,而Post请求则将参数隐藏在Header实体中。通过模拟这两种请求,可以实现对网页内容的下载和信息提取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

今天,首先介绍下怎么抓取网页上的数据。在任何一个网页上,我们可以右键查看网页源代码,看到网页上的各元素。实际上,我们每次向服务器发起请求的时候,服务器正是返回给我们这些数据,而浏览器的功能则是解析这些数据,展现给我们看。爬虫程序正是把这些个数据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中直接填写(百度首页这个例子没有体现)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值