Spider网络爬虫学习——通过Get、Post方法直接抓取网页内容
来源:互联网 发布:大数据培训 编辑:程序博客网 时间:2024/06/05 00:56
网络爬虫(又被称为网页蜘蛛,网络机器人),是一种按照一定的规则,自动的抓取万维网信息的程序或者脚本。另外一些不常使用的名字还有蚂蚁,自动索引,模拟程序或者蠕虫。我们常用网络爬虫程序,在网络上抓取自己感兴趣的信息,那么网络爬虫程序究竟是怎么写的呢?接下来将一步步展开介绍。
今天,首先介绍下怎么抓取网页上的数据。在任何一个网页上,我们可以右键查看网页源代码,看到网页上的各元素。实际上,我们每次向服务器发起请求的时候,服务器正是返回给我们这些数据,而浏览器的功能则是解析这些数据,展现给我们看。爬虫程序正是把这些个数据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;否则为失败,返回falseif(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;}elsereturn 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;否则为失败,返回falseif(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;}elsereturn false;}/** * 主方法 * * @param args * @throws HttpException * @throws IOException */public static void main(String[] args) throws HttpException, IOException {// Get请求URLString getURL = "http://www.baidu.com";// 百度首页// Post请求URLString postURL = "http://xxxxxxxxxxx";// 自己找一个post的网站// 通过Get请求方法抓取网页内容downloadPageByGet(getURL);// 通过Post请求方法抓取网页内容downloadPageByPost(postURL);}}downloadPageByGet( )和downloadPageByPost( )这两个方法正是模拟了抓取内容的过程。可以发现,Post请求的时候,需要设置参数;而Get请求的时候没有设置,而是直接URL中直接填写(百度首页这个例子没有体现)
0 0
- Spider网络爬虫学习——通过Get、Post方法直接抓取网页内容
- pytyhon抓取网页内容get post方法
- Linux企业级项目实践之网络爬虫(9)——通过URL抓取网页内容
- python网络爬虫学习(一)通过GET和POST方式获取页面内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- 网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- 网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- 网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- JQuery制作的选项卡改进版
- getRequestDispatcher(path).forward(),,执行完,后面的代码居然还会执行!!!
- 队列的顺序存储及实现(C++版)
- Sql 行转换列(列转换行), JavaScript解决思路
- JavaFX编译器编译满载方法的一处BUG
- Spider网络爬虫学习——通过Get、Post方法直接抓取网页内容
- eBay1亿4千万用户资料恐外泄!五个问题请教 eBay…
- spring 事宜方法嵌套调用的迷茫
- 那种视频文件最清晰
- 请遵守好的编程风格 -- 关于.h文件去写函数的具体实现
- LeetCode- Path Sum
- jQuery 获取屏幕高度、宽度
- RaceWeb介绍(1):基本功能
- 清除浮动技巧总结