整个的宽度优先爬虫过程就是从一系列的种子节点开始,把这些网页中(种子结点网页)的“子节点” (也就是超链接)提取出来,放入队列中依次进行抓取。被处理过的链接需要放入一张表(通常称 为 Visited 表)中。每次新处理一个链接之前,需要查看这个链接是否已经存在于 Visited 表 中。如果存在,证明链接已经处理过,跳过,不做处理,否则进行下一步处理。实际的过 程如图 1.5 所示。
初始的 URL 地址是爬虫系统中提供的种子 URL(一般在系统的配置文件中指定)。当解析这些种子 URL 所表示的网页时,会产生新的 URL(比如从页面中的<a href= “http://www.admin.com”中提取出 http://www.admin.com 这个链接)。然后,进行以下工作:
(1) 把解析出的链接和 Visited 表中的链接进行比较,若 Visited 表中不存在此链接,表示其未被访问过。
(2) 把链接放入 TODO 表(TODO表用于存放未访问的链接URL)中。
(3) 处理完毕后,再次从 TODO 表中得一条链接,直接放入 Visited 表中。
(4) 针对这个链接所表示的网页,继续上述过程。如此循环往复。
表 1.3 显示了对图 1.3 所示的页面的爬取过程。
JAVA代码实现:
(1)Queue类:
/** * 队列,保存将要访问的URL */
public class Queue {
//使用链表实现队列
private LinkedList queue = new LinkedList();
//入队列
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。 每当要访问一个 URL 的时候,首先在这个数据结构中进行查找,如果当前的 URL 已经存在,则丢弃它。
(2) LinkQueue类:<br><br>public class LinkQueue {
//已访问的 url 集合
private static Set visitedUrl = new HashSet();
//待访问的 url 集合
private static Queue unVisitedUrl = new Queue();
//获得URL队列
public static Queue getUnVisitedUrl() {
return unVisitedUrl;
}
//添加到访问过的URL队列中
public static void addVisitedUrl(String url) {
visitedUrl.add(url);
}
//移除访问过的URL
public static void removeVisitedUrl(String url) {
visitedUrl.remove(url);
}
//未访问的URL出队列
public static Object unVisitedUrlDeQueue() {
return unVisitedUrl.deQueue();
}
// 保证每个 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 int getVisitedUrlNum() {
return visitedUrl.size();
}
//判断未访问的URL队列中是否为空
public static boolean unVisitedUrlsEmpty() {
return unVisitedUrl.empty();
}
}
下面的代码详细说明了网页下载并处理的过程。比如如何存储网页,设置请求超时策略等。
(3) DownLoadFile类:
public class DownLoadFile {
/** * 根据 URL 和网页类型生成需要保存的网页的文件名,去除 URL 中的非文件名字符 */
public String getFileNameByUrl(String url,String contentType) {
//移除http: <br> url=url.substring(7);
//text/html类型
if(contentType.indexOf("html")!=-1)
{
url= url.replaceAll("[\\?/:*|<>\"]", "_")+".html";
return url;
}
//如application/pdf类型
else { return url.replaceAll("[\\?/:*|<>\"]", "_")+"."+ contentType.substring(contentType.lastIndexOf("/")+1);
}<br>
}
/** * 保存网页字节数组到本地文件,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 <br>httpClient.getHttpConnectionManager().getParam().setConnectionTimeout(5000);
// 2.生成 GetMethod 对象并设置参数
GetMethod getMethod = new GetMethod(url);
// 设置 get 请求超时 5s
getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,5000);<br>
// 设置请求重试处理 getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
// 3.执行 HTTP GET 请求
try { int statusCode = httpClient.executeMethod(getMethod); <br><br>// 判断访问的状态码 if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "+ getMethod.getStatusLine());
filePath = null;
}<br><br>
// 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("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e) {
// 发生网络异常
e.printStackTrace();
} finally {
// 释放连接 getMethod.releaseConnection();
}
}
} return filePath;
}
}
(4)Java 有一个非常实用的开源工具包 HtmlParser,它专门针对 Html 页面进行处理,不仅能提取 URL,还能提取文本以及你想要的任何内容.下面的HtmlParserTool类将实现其具体的一些功能:
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() {
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 属性的链接,如 <framesrc="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;
}
}
(5)现在使用MyCrawler类将爬虫爬起来: