首页 > 解决方案 > 获取文本中的单词数

问题描述

我正在使用 Java 和 Selenium,我必须提取特定文本中的单词数。我被困住了,因为我得到的结果比我预期的要多。

考虑以下 HTML

    <div data-v-2f952c88="" class="text1">
 <section data-v-3b70ad5b="" data-v-2f952c88="" data-content-provider="ABC" class="description__section">
   <div data-v-051a83e7="" data-v-3b70ad5b="" class="markdown" data-v-2f952c88="">
     <p>Headline 1
       Hello everyone i´m new at stack overflow</p>
     <p> And I need your help
        to get the total of words in this exemple
     </p>
   </div>
 </section>
 <section data-v-3b70ad5b="" data-v-2f952c88="" data-content-provider="DEF" class="description__section">
     <div data-v-051a83e7="" data-v-3b70ad5b="" class="markdown" data-v-2f952c88="">
        <p>I Love Coding
            I use Java</p>
        <p> Another Text
            And Selenium
        </p>
    </div>
  </section>
</div>

<div data-v-2f952c99="" class="querty">
 <section data-v-3b755ad5b="" data-v-2f952288="" data-content-provider="DEF" class="description__section">
   <div data-v-051a18e7="" data-v-3b789d5b="" class="markdown" data-v-2f962c88="">
     <p>This is another text along the WEBPAGE
       I don´t want to count this words in my total count</p>
    </div>
 </section>
</div>

在 Java 中,我创建了这个函数:

    private String countWords(WebDriver driver){        
    int totalLetters = 0;     
        try{                               
            List<WebElement> className = driver.findElements(By.cssSelector("[class*='text1']"));
            for(WebElement classElement: className){
                if(classElement!=null) {
                    String[] tags = {"p", "section"};
                    for (String tag: tags) {
                        List<WebElement> elements = driver.findElements(By.tagName(tag));
                        for (WebElement element: elements) {
                            String text=element.getText();                        
                            String[] words = text.split("\\s+");                        
                            if (words!=null) {                            
                                totalLetters = totalLetters + words.length;                            
                            }
                        }
                    }
                }
            }
        }
    
        catch(NoSuchMethodError e){
            //e.printStackTrace();
            throw e;
        }
    String s=String.valueOf(totalLetters);
    System.out.println("How many word? " + s);
    return s;

所以我的问题是我的功能是提取网页中每个“p”和“section”标签中的所有单词,我只想要第一个“div ..... class =”中的“p”和“section”文本1” ”。

我究竟做错了什么?

标签: javaselenium

解决方案


请参考图片以检查为什么它会计算所有“p”和“section”标签 在此处输入图像描述

这有助于找到您的问题吗?或者你的问题是它也给出了class ='querty'的计数?

<div data-v-2f952c99="" class="querty">
 <section data-v-3b755ad5b="" data-v-2f952288="" data-content-provider="DEF" class="description__section">
   <div data-v-051a18e7="" data-v-3b789d5b="" class="markdown" data-v-2f962c88="">
     <p>This is another text along the WEBPAGE
       I don´t want to count this words in my total count</p>
    </div>
 </section>
</div>

推荐阅读