歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 基於Java的寬度優先遍歷互聯網結點

基於Java的寬度優先遍歷互聯網結點

日期:2017/3/1 9:11:49   编辑:Linux編程

整個的寬度優先爬蟲過程就是從一系列的種子節點開始,把這些網頁中(種子結點網頁)的“子節點” (也就是超鏈接)提取出來,放入隊列中依次進行抓取。被處理過的鏈接需要放入一張表(通常稱 為 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類將爬蟲爬起來:

public class MyCrawler {
/** * 使用種子初始化 URL 隊列
* @return * @param seeds 種子URL */
private void initCrawlerWithSeeds(String[] seeds) {
for(int i=0;i<seeds.length;i++)
LinkQueue.addUnvisitedUrl(seeds[i]);
}


/** * 抓取過程 * @return * @param seeds */
public void crawling(String[] seeds) {
//定義過濾器,提取以http://www.linuxidc.com開頭的鏈接
LinkFilter filter = new LinkFilter(){
public boolean accept(String url) {
if(url.startsWith("http://www.linuxidc.com"))
return true;
else
return false;
}
}

//初始化 URL 隊列
initCrawlerWithSeeds(seeds);
//循環條件:待抓取的鏈接不空且抓取的網頁不多於1000
while(!LinkQueue.unVisitedUrlsEmpty() &&LinkQueue.getVisitedUrlNum()<=1000) {
//隊頭URL出隊列
String visitUrl=(String)LinkQueue.unVisitedUrlDeQueue();
if(visitUrl==null) continue;
DownLoadFile downLoader=new DownLoadFile();
//下載網頁 downLoader.downloadFile(visitUrl);
//該 URL 放入已訪問的 URL 中 LinkQueue.addVisitedUrl(visitUrl);
//提取出下載網頁中的
URL Set<String> links=HtmlParserTool.extracLinks(visitUrl,filter);
//新的未訪問的 URL 入隊
for(String link:links) {
LinkQueue.addUnvisitedUrl(link);
}
}
}
//main 方法入口
public static void main(String[]args) {
MyCrawler crawler = new MyCrawler();
crawler.crawling(new String[]{"http://www.linuxidc.com"});
}

Copyright © Linux教程網 All Rights Reserved