首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >让网页爬虫解析网站

让网页爬虫解析网站
EN

Stack Overflow用户
提问于 2015-11-10 23:56:08
回答 1查看 66关注 0票数 1

所以今天早些时候我发布了Finding number of occurrences on website

但我得到的答案并没有我所希望的那么有帮助。我试着告诉爬虫从它找到的网站上阅读文本,并搜索一个给定的单词。我发现这件事

代码语言:javascript
复制
 org.jsoup.nodes.Document dom = Jsoup.parse(html);

然而,我不知道如何实现它。请帮帮忙

履带机

代码语言:javascript
复制
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;
}

搜索者

代码语言:javascript
复制
public int searchHits(String target, String aften){ // TODO
    String[] out = new String[0];
    int occurrences = 0;

    if (aften.contains(target)) {
       occurrences++;
    }
    return occurrences;
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-11 00:32:33

我不太清楚aftentarget是什么,但我将给您一个搜索文本中单词出现情况的代码片段。

代码语言:javascript
复制
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);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33641925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档