更新時間:2022-11-18 14:49:15 來源:動力節(jié)點 瀏覽1603次
我們將構(gòu)建一個使用 BFS 算法遍歷網(wǎng)頁的網(wǎng)絡(luò)爬蟲。
爬蟲將從訪問包含的每個 URL 的源 URL 開始。訪問此源 URL 中的每個 URL 后,該算法將訪問子 URL 中的每個 URL 并向下訪問鏈,直到它到達(dá)您將指定的斷點。
該算法將僅訪問以前未訪問過的 URL,以確保我們不會循環(huán)訪問。這些 URL 代表頂點,URL 之間的連接是邊。
首先將根 URL 添加到隊列和已訪問 URL 列表。
當(dāng)隊列不為空時,從隊列中刪除 URL 并讀取其原始 HTML 內(nèi)容。
在讀取 URL 的 HTML 內(nèi)容時,搜索父 HTML 中包含的任何其他 URL。
當(dāng)找到新的 URL 時,通過檢查已訪問的 URL 列表來驗證它以前沒有被訪問過。
將新找到的未訪問 URL 添加到隊列和已訪問 URL 列表中。
對在 HTML 內(nèi)容中找到的每個新 URL 重復(fù)步驟 4 和 5。
找到 HTML 中的所有 URL 后,從步驟 2 開始重復(fù),直到程序到達(dá)您指定的斷點。
使用名稱創(chuàng)建一個 Java 類WebCrawler并將以下代碼添加到文件中:
public class WebCrawler {
private Queue<String> urlQueue;
private List<String> visitedURLs;
public WebCrawler() {
urlQueue = new LinkedList<>();
visitedURLs = new ArrayList<>();
}
}
在上面的代碼中,我們已經(jīng)初始化了我們隨后將要使用的類、數(shù)據(jù)結(jié)構(gòu)和構(gòu)造函數(shù)。
public void crawl(String rootURL, int breakpoint) {
urlQueue.add(rootURL);
visitedURLs.add(rootURL);
while(!urlQueue.isEmpty()){
// remove the next url string from the queue to begin traverse.
String s = urlQueue.remove();
String rawHTML = "";
try{
// create url with the string.
URL url = new URL(s);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine = in.readLine();
// read every line of the HTML content in the URL
// and concat each line to the rawHTML string until every line is read.
while(inputLine != null){
rawHTML += inputLine;
inputLine = in.readLine();
}
in.close();
} catch (Exception e){
e.printStackTrace();
}
// create a regex pattern matching a URL
// that will validate the content of HTML in search of a URL.
String urlPattern = "(www|http:|https:)+[^\s]+[\\w]";
Pattern pattern = Pattern.compile(urlPattern);
Matcher matcher = pattern.matcher(rawHTML);
// Each time the regex matches a URL in the HTML,
// add it to the queue for the next traverse and the list of visited URLs.
breakpoint = getBreakpoint(breakpoint, matcher);
// exit the outermost loop if it reaches the breakpoint.
if(breakpoint == 0){
break;
}
}
}
在該crawl()方法中,rootURL是爬蟲的起點,breakpoint代表您希望爬蟲發(fā)現(xiàn)多少個 URL。
該算法涉及的步驟是:
該算法首先將根 URL 添加到隊列和已訪問 URL 列表。
BufferedReader它使用API讀取 URL 的每一行 HTML 內(nèi)容。
然后,它在讀取rawHTML變量時連接每個 HTML 行。
private int getBreakpoint(int breakpoint, Matcher matcher) {
while(matcher.find()){
String actualURL = matcher.group();
if(!visitedURLs.contains(actualURL)){
visitedURLs.add(actualURL);
System.out.println("Website found with URL " + actualURL);
urlQueue.add(actualURL);
}
// exit the loop if it reaches the breakpoint.
if(breakpoint == 0){
break;
}
breakpoint--;
}
return breakpoint;
}
上面的代碼執(zhí)行以下操作:
該getBreakPoint方法使用指定的正則表達(dá)式模式來發(fā)現(xiàn)rawHTML.
這些操作會不斷迭代,直到爬蟲發(fā)現(xiàn)了您的breakpoint.
以下是應(yīng)用程序主要方法的片段:
public static void main(String[] args) {
WebCrawler crawler = new WebCrawler();
String rootURL = "https://www.section.io/engineering-education/springboot-antmatchers/";
crawler.crawl(rootURL, 100);
}
下面是運行該程序的輸出快照:
上面快照中的 URL 是網(wǎng)絡(luò)爬蟲從根 URL 開始通過嵌入 URL 爬取到斷點的所有網(wǎng)頁中包含的一些 URL。如果大家想了解更多相關(guān)知識,不妨來關(guān)注一下本站的Java在線學(xué)習(xí),里面的課程內(nèi)容從入門到精通,細(xì)致全面,通俗易懂,很適合沒有基礎(chǔ)的小伙伴學(xué)習(xí),希望對大家能夠有所幫助。
相關(guān)閱讀
初級 202925
初級 203221
初級 202629
初級 203743