所以今天早些时候我发布了Finding number of occurrences on website
但我得到的答案并没有我所希望的那么有帮助。我试着告诉爬虫从它找到的网站上阅读文本,并搜索一个给定的单词。我发现这件事
org.jsoup.nodes.Document dom = Jsoup.parse(html);然而,我不知道如何实现它。请帮帮忙
履带机
public void crawlFrom(String link){ // TODO
try
{
Connection connection = Jsoup.connect(link).userAgent(USER_AGENT);
Document htmlDocument = connection.get();
this.htmlDocument = htmlDocument;
System.out.println("Received web page at " + link);
Elements linksOnPage = htmlDocument.select("a[href]");
System.out.println("------------------\nFound (" + linksOnPage.size() + ") links\n------------------");
for(Element newLink : linksOnPage)
{
this.linkListe.add(newLink.absUrl("href"));
}
}
catch(IOException ioe)
{
// We were not successful in our HTTP request
System.out.println("Error in out HTTP request " + ioe);
}
System.out.println(linkListe);
return;
}搜索者
public int searchHits(String target, String aften){ // TODO
String[] out = new String[0];
int occurrences = 0;
if (aften.contains(target)) {
occurrences++;
}
return occurrences;
}发布于 2015-11-11 00:32:33
我不太清楚aften和target是什么,但我将给您一个搜索文本中单词出现情况的代码片段。
public int searchHits(String target, String aften){ // TODO
int index = 0;
int occurrences = 0;
while(index != -1){
index = aften.indexOf(target,index); // start search from index 0
if(index != -1){
occurrences ++; //if found, increment the counter
index += target.length(); // set the next starting index to be after this current index
}
}
return occurrences;
}将抽象方法从int searchHits(String word);更改为int searchHits(String target, String aften);
https://stackoverflow.com/questions/33641925
复制相似问题