当前位置: 移动技术网 > IT编程>开发语言>Java > 零基础写Java知乎爬虫之进阶篇

零基础写Java知乎爬虫之进阶篇

2019年07月22日  | 移动技术网IT编程  | 我要评论

说到爬虫,使用java本身自带的urlconnection可以实现一些基本的抓取页面的功能,但是对于一些比较高级的功能,比如重定向的处理,html标记的去除,仅仅使用urlconnection还是不够的。

在这里我们可以使用httpclient这个第三方jar包。

接下来我们使用httpclient简单的写一个爬去百度的demo:

import java.io.fileoutputstream;
import java.io.inputstream;
import java.io.outputstream;
import org.apache.commons.httpclient.httpclient;
import org.apache.commons.httpclient.httpstatus;
import org.apache.commons.httpclient.methods.getmethod;
/**
 *
 * @author callmewhy
 *
 */
public class spider {
 private static httpclient httpclient = new httpclient();
 /**
  * @param path
  *            目标网页的链接
  * @return 返回布尔值,表示是否正常下载目标页面
  * @throws exception
  *             读取网页流或写入本地文件流的io异常
  */
 public static boolean downloadpage(string path) throws exception {
  // 定义输入输出流
  inputstream input = null;
  outputstream output = null;
  // 得到 post 方法
  getmethod getmethod = new getmethod(path);
  // 执行,返回状态码
  int statuscode = httpclient.executemethod(getmethod);
  // 针对状态码进行处理
  // 简单起见,只处理返回值为 200 的状态码
  if (statuscode == httpstatus.sc_ok) {
   input = getmethod.getresponsebodyasstream();
   // 通过对url的得到文件名
   string filename = path.substring(path.lastindexof('/') + 1)
     + ".html";
   // 获得文件输出流
   output = new fileoutputstream(filename);
   // 输出到文件
   int tempbyte = -1;
   while ((tempbyte = input.read()) > 0) {
    output.write(tempbyte);
   }
   // 关闭输入流
   if (input != null) {
    input.close();
   }
   // 关闭输出流
   if (output != null) {
    output.close();
   }
   return true;
  }
  return false;
 }
 public static void main(string[] args) {
  try {
   // 抓取百度首页,输出
   spider.downloadpage("http://www.baidu.com");
  } catch (exception e) {
   e.printstacktrace();
  }
 }
}


但是这样基本的爬虫是不能满足各色各样的爬虫需求的。

先来介绍宽度优先爬虫。

宽度优先相信大家都不陌生,简单说来可以这样理解宽度优先爬虫。

我们把互联网看作一张超级大的有向图,每一个网页上的链接都是一个有向边,每一个文件或没有链接的纯页面则是图中的终点:


宽度优先爬虫就是这样一个爬虫,爬走在这个有向图上,从根节点开始一层一层往外爬取新的节点的数据。

宽度遍历算法如下所示:

(1) 顶点 v 入队列。
(2) 当队列非空时继续执行,否则算法为空。
(3) 出队列,获得队头节点 v,访问顶点 v 并标记 v 已经被访问。
(4) 查找顶点 v 的第一个邻接顶点 col。
(5) 若 v 的邻接顶点 col 未被访问过,则 col 进队列。
(6) 继续查找 v 的其他邻接顶点 col,转到步骤(5),若 v 的所有邻接顶点都已经被访问过,则转到步骤(2)。

按照宽度遍历算法,上图的遍历顺序为:a->b->c->d->e->f->h->g->i,这样一层一层的遍历下去。

而宽度优先爬虫其实爬取的是一系列的种子节点,和图的遍历基本相同。

我们可以把需要爬取页面的url都放在一个todo表中,将已经访问的页面放在一个visited表中:


则宽度优先爬虫的基本流程如下:

(1) 把解析出的链接和 visited 表中的链接进行比较,若 visited 表中不存在此链接, 表示其未被访问过。
(2) 把链接放入 todo 表中。
(3) 处理完毕后,从 todo 表中取得一条链接,直接放入 visited 表中。
(4) 针对这个链接所表示的网页,继续上述过程。如此循环往复。

下面我们就来一步一步制作一个宽度优先的爬虫。

首先,对于先设计一个数据结构用来存储todo表, 考虑到需要先进先出所以采用队列,自定义一个quere类:

import java.util.linkedlist;
/**
 * 自定义队列类 保存todo表
 */
public class queue {
 /**
  * 定义一个队列,使用linkedlist实现
  */
 private linkedlist<object> queue = new linkedlist<object>(); // 入队列
 /**
  * 将t加入到队列中
  */
 public void enqueue(object t) {
  queue.addlast(t);
 }
 /**
  * 移除队列中的第一项并将其返回
  */
 public object dequeue() {
  return queue.removefirst();
 }
 /**
  * 返回队列是否为空
  */
 public boolean isqueueempty() {
  return queue.isempty();
 }
 /**
  * 判断并返回队列是否包含t
  */
 public boolean contians(object t) {
  return queue.contains(t);
 }
 /**
  * 判断并返回队列是否为空
  */
 public boolean empty() {
  return queue.isempty();
 }
}



还需要一个数据结构来记录已经访问过的 url,即visited表。

考虑到这个表的作用,每当要访问一个 url 的时候,首先在这个数据结构中进行查找,如果当前的 url 已经存在,则丢弃这个url任务。

这个数据结构需要不重复并且能快速查找,所以选择hashset来存储。

综上,我们另建一个spiderqueue类来保存visited表和todo表:

import java.util.hashset;
import java.util.set;
/**
 * 自定义类 保存visited表和unvisited表
 */
public class spiderqueue {
 /**
  * 已访问的url集合,即visited表
  */
 private static set<object> visitedurl = new hashset<>();
 /**
  * 添加到访问过的 url 队列中
  */
 public static void addvisitedurl(string url) {
  visitedurl.add(url);
 }
 /**
  * 移除访问过的 url
  */
 public static void removevisitedurl(string url) {
  visitedurl.remove(url);
 }
 /**
  * 获得已经访问的 url 数目
  */
 public static int getvisitedurlnum() {
  return visitedurl.size();
 }
 /**
  * 待访问的url集合,即unvisited表
  */
 private static queue unvisitedurl = new queue();
 /**
  * 获得unvisited队列
  */
 public static queue getunvisitedurl() {
  return unvisitedurl;
 }
 /**
  * 未访问的unvisitedurl出队列
  */
 public static object unvisitedurldequeue() {
  return unvisitedurl.dequeue();
 }
 /**
  * 保证添加url到unvisitedurl的时候每个 url只被访问一次
  */
 public static void addunvisitedurl(string url) {
  if (url != null && !url.trim().equals("") && !visitedurl.contains(url)
    && !unvisitedurl.contians(url))
   unvisitedurl.enqueue(url);
 }
 /**
  * 判断未访问的 url队列中是否为空
  */
 public static boolean unvisitedurlsempty() {
  return unvisitedurl.empty();
 }
}


上面是一些自定义类的封装,接下来就是一个定义一个用来下载网页的工具类,我们将其定义为downtool类:

package controller;
import java.io.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.*;
public class downtool {
 /**
  * 根据 url 和网页类型生成需要保存的网页的文件名,去除 url 中的非文件名字符
  */
 private string getfilenamebyurl(string url, string contenttype) {
  // 移除 "http://" 这七个字符
  url = url.substring(7);
  // 确认抓取到的页面为 text/html 类型
  if (contenttype.indexof("html") != -1) {
   // 把所有的url中的特殊符号转化成下划线
   url = url.replaceall("[\\?/:*|<>\"]", "_") + ".html";
  } else {
   url = url.replaceall("[\\?/:*|<>\"]", "_") + "."
     + contenttype.substring(contenttype.lastindexof("/") + 1);
  }
  return url;
 }
 /**
  * 保存网页字节数组到本地文件,filepath 为要保存的文件的相对地址
  */
 private void savetolocal(byte[] data, string filepath) {
  try {
   dataoutputstream out = new dataoutputstream(new fileoutputstream(
     new file(filepath)));
   for (int i = 0; i < data.length; i++)
    out.write(data[i]);
   out.flush();
   out.close();
  } catch (ioexception e) {
   e.printstacktrace();
  }
 }
 // 下载 url 指向的网页
 public string downloadfile(string url) {
  string filepath = null;
  // 1.生成 httpclinet对象并设置参数
  httpclient httpclient = new httpclient();
  // 设置 http连接超时 5s
  httpclient.gethttpconnectionmanager().getparams()
    .setconnectiontimeout(5000);
  // 2.生成 getmethod对象并设置参数
  getmethod getmethod = new getmethod(url);
  // 设置 get请求超时 5s
  getmethod.getparams().setparameter(httpmethodparams.so_timeout, 5000);
  // 设置请求重试处理
  getmethod.getparams().setparameter(httpmethodparams.retry_handler,
    new defaulthttpmethodretryhandler());
  // 3.执行get请求
  try {
   int statuscode = httpclient.executemethod(getmethod);
   // 判断访问的状态码
   if (statuscode != httpstatus.sc_ok) {
    system.err.println("method failed: "
      + getmethod.getstatusline());
    filepath = null;
   }
   // 4.处理 http 响应内容
   byte[] responsebody = getmethod.getresponsebody();// 读取为字节数组
   // 根据网页 url 生成保存时的文件名
   filepath = "temp\\"
     + getfilenamebyurl(url,
       getmethod.getresponseheader("content-type")
         .getvalue());
   savetolocal(responsebody, filepath);
  } catch (httpexception e) {
   // 发生致命的异常,可能是协议不对或者返回的内容有问题
   system.out.println("请检查你的http地址是否正确");
   e.printstacktrace();
  } catch (ioexception e) {
   // 发生网络异常
   e.printstacktrace();
  } finally {
   // 释放连接
   getmethod.releaseconnection();
  }
  return filepath;
 }
}

在这里我们需要一个htmlparsertool类来处理html标记:

package controller;
import java.util.hashset;
import java.util.set;
import org.htmlparser.node;
import org.htmlparser.nodefilter;
import org.htmlparser.parser;
import org.htmlparser.filters.nodeclassfilter;
import org.htmlparser.filters.orfilter;
import org.htmlparser.tags.linktag;
import org.htmlparser.util.nodelist;
import org.htmlparser.util.parserexception;
import model.linkfilter;
public class htmlparsertool {
 // 获取一个网站上的链接,filter 用来过滤链接
 public static set<string> extraclinks(string url, linkfilter filter) {
  set<string> links = new hashset<string>();
  try {
   parser parser = new parser(url);
   parser.setencoding("gb2312");
   // 过滤 <frame >标签的 filter,用来提取 frame 标签里的 src 属性
   nodefilter framefilter = new nodefilter() {
    private static final long serialversionuid = 1l;
    @override
    public boolean accept(node node) {
     if (node.gettext().startswith("frame src=")) {
      return true;
     } else {
      return false;
     }
    }
   };
   // orfilter 来设置过滤 <a> 标签和 <frame> 标签
   orfilter linkfilter = new orfilter(new nodeclassfilter(
     linktag.class), framefilter);
   // 得到所有经过过滤的标签
   nodelist list = parser.extractallnodesthatmatch(linkfilter);
   for (int i = 0; i < list.size(); i++) {
    node tag = list.elementat(i);
    if (tag instanceof linktag)// <a> 标签
    {
     linktag link = (linktag) tag;
     string linkurl = link.getlink();// url
     if (filter.accept(linkurl))
      links.add(linkurl);
    } else// <frame> 标签
    {
     // 提取 frame 里 src 属性的链接, 如 <frame src="test.html"/>
     string frame = tag.gettext();
     int start = frame.indexof("src=");
     frame = frame.substring(start);
     int end = frame.indexof(" ");
     if (end == -1)
      end = frame.indexof(">");
     string frameurl = frame.substring(5, end - 1);
     if (filter.accept(frameurl))
      links.add(frameurl);
    }
   }
  } catch (parserexception e) {
   e.printstacktrace();
  }
  return links;
 }
}

最后我们来写个爬虫类调用前面的封装类和函数:

package controller;
import java.util.set;
import model.linkfilter;
import model.spiderqueue;
public class bfsspider {
 /**
  * 使用种子初始化url队列
  */
 private void initcrawlerwithseeds(string[] seeds) {
  for (int i = 0; i < seeds.length; i++)
   spiderqueue.addunvisitedurl(seeds[i]);
 }
 // 定义过滤器,提取以 http://www.xxxx.com开头的链接
 public void crawling(string[] seeds) {
  linkfilter filter = new linkfilter() {
   public boolean accept(string url) {
    if (url.startswith("http://www.baidu.com"))
     return true;
    else
     return false;
   }
  };
  // 初始化 url 队列
  initcrawlerwithseeds(seeds);
  // 循环条件:待抓取的链接不空且抓取的网页不多于 1000
  while (!spiderqueue.unvisitedurlsempty()
    && spiderqueue.getvisitedurlnum() <= 1000) {
   // 队头 url 出队列
   string visiturl = (string) spiderqueue.unvisitedurldequeue();
   if (visiturl == null)
    continue;
   downtool downloader = new downtool();
   // 下载网页
   downloader.downloadfile(visiturl);
   // 该 url 放入已访问的 url 中
   spiderqueue.addvisitedurl(visiturl);
   // 提取出下载网页中的 url
   set<string> links = htmlparsertool.extraclinks(visiturl, filter);
   // 新的未访问的 url 入队
   for (string link : links) {
    spiderqueue.addunvisitedurl(link);
   }
  }
 }
 // main 方法入口
 public static void main(string[] args) {
  bfsspider crawler = new bfsspider();
  crawler.crawling(new string[] { "http://www.baidu.com" });
 }
}

运行可以看到,爬虫已经把百度网页下所有的页面都抓取出来了:

以上就是java使用httpclient工具包和宽度爬虫进行抓取内容的操作的全部内容,稍微复杂点,小伙伴们要仔细琢磨下哦,希望对大家能有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网