嗨!我正在尝试为蜘蛛算法实现这个伪代码来探索网络.
我需要一些关于伪代码下一步的想法:“使用SpiderLeg来获取内容”,
我在另一个类SpiderLeg中有一个方法,它有一个方法来获取该网页的所有URL,但想知道如何在这个类中使用它?
// method to crawl web and print out all URLs that the spider visit
public List crawl(String url, String keyword) throws IOException{
String currentUrl;
// while list of unvisited URLs is not empty
while(unvisited != null ){
// take URL from list
currentUrl = unvisited.get(0);
//using spiderLeg to fetch content
SpiderLeg leg = new SpiderLeg();
}
return unvisited;
}
干杯!!将尝试…但是我尝试了这个没有使用队列D.S,它几乎工作,但不会在搜索某些单词时停止程序.
当它发现它只显示网页的链接而不是它找到该单词的所有特定URL.
想知道可以这样做吗?
private static final int MAX_PAGES_TO_SEARCH = 10;
private Set pagesVisited = new HashSet();
private List pagesToVisit = new LinkedList();
public void crawl(String url, String searchWord)
{
while(this.pagesVisited.size() < MAX_PAGES_TO_SEARCH)
{
String currentUrl;
SpiderLeg leg = new SpiderLeg();
if(this.pagesToVisit.isEmpty())
{
currentUrl = url;
this.pagesVisited.add(url);
}
else
{
currentUrl = this.nextUrl();
}
leg.getHyperlink(currentUrl);
boolean success = leg.searchForWord(searchWord);
if(success)
{
System.out.println(String.format("**Success** Word %s found at %s", searchWord, currentUrl));
break;
}
this.pagesToVisit.addAll(leg.getLinks());
}
System.out.println("\n**Done** Visited " + this.pagesVisited.size() + " web page(s)");
}