java - extracting a element from jsoup for a text value match in the element attribute
问题描述
How do I get the span with a certain text within an attribute? I am trying to extract the number that comes after the text "stars". So how can I select a span tag that has text "rating_sprite stars" and I want the value "star5" to be extracted from the attribute so that I can get 5 out of the text.
Currently I dont get any elements back!
String url = "https://www.morningstar.co.uk/uk/funds/snapshot/snapshot.aspx?id=F00000WYA1";
Document doc = null;
try {
doc = Jsoup.connect(url).get();
} catch (IOException e) {
e.printStackTrace();
}
Elements spans = doc.select("span.rating_sprite");
System.out.println(spans);
the HTML snippet looks something like this
<div class="snapshotTitleBox">
<h1>Comgest</h1>
<span class="rating_sprite stars5"></span>
<span class="rating_sprite analyst-rating-5"></span>
<div style="float:right; margin-top:6px;"></div>
</div>
解决方案
仅使用 jsoup 无法做到这一点。但是您已经拥有了所有span.rating_sprite
元素,因此您可以遍历它们中的每一个并找到一个与类匹配的正则表达式stars(\d)
。然后您可以捕获仅包含数字的第一组:
Pattern p = Pattern.compile("stars(\\d)");
for (Element span : spans) {
for (String className : span.classNames()) {
Matcher m = p.matcher(className);
if (m.matches()) {
System.out.println("stars: " + m.group(1));
}
}
}
推荐阅读
- javascript - Firestore 用户 ID 与文档 ID 不同,反应原生 Firebase
- django - 如何在git中推送单个文件
- ionic-framework - 显示相对于元素的 Ionic 弹出框而不单击
- html - 预加载顺序重要吗?
- qt - Qt QML属性绑定和属性改变信号执行顺序
- angular - 如何在 Angular 11 中为数据创建模板上下文
- function - Sympy 函数无法识别有关变量的假设
- string - Groovy 多行字符串保持新行和缩进
- python - 如何使用 if 语句在我的脚本中创建条件?
- python - 使用 **kwargs 看到的 mypy 中的不兼容类型