【问题标题】:Get number of words in a text获取文本中的单词数
【发布时间】:2020-10-13 20:16:47
【问题描述】:

我正在使用 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”中的“p”和“section”..... class="text1" "。

我做错了什么?

【问题讨论】:

标签: java selenium


【解决方案1】:

请参考图片来检查为什么它给出了所有'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>

【讨论】:

    猜你喜欢
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    相关资源
    最近更新 更多