问题描述
我试图找出关键字与网页中单词总数的比率,我正在使用jsoup来解析网页的HTML.我想知道如何使用JSOUP找出网页中关键字的数量.我想知道JSOUP中是否有一个函数可以做到这一点!
I am trying to find out the keyword to total number of words ratio in a webpage, I am using jsoup to parse the HTML of the webpages. I want to know how to find out the count of a keyword in a webpage using JSOUP. I want to know if there is a function in JSOUP to do that Thanks!
推荐答案
我不认为Jsoup中有计算字数的方法,因为JSoup仅用于解析html/xml.如果可以接受所有文本(包括由于诸如隐藏之类的CSS规则可能无法渲染的内容),则可以简单地使用Element.text().有关详细信息,请参见此处.
I don't think there is a methods for counting words in Jsoup, since JSoup is about parsing html/xml only. If you are okay with getting all text (including stuff that might not be rendered due to css rules like hiding) you can simply use Element.text(). See here for details.
Element body = doc.body();
String allText = body.text();
现在,您可以使用allText
中的字符串进行各种文字处理.好的文字处理库可能是 apache lucene 或更简单的东西,例如 Wordcounter
Now you can do all sorts of word processing with the string in allText
. Good libraries for word processing might be apache lucene or simpler stuff like Wordcounter
这篇关于使用jsoup查找HTML中的关键字计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!