java - 使用 Jsoup 非递归提取文本
问题描述
这是我要运行的代码:
String html = "<a href=\"/name/zola-1\">ZOLA <span class=\"tiny\">(1)</span></a>";
Document doc = Jsoup.parse(html); //connect to the page
Element element = doc.getAllElements().first(); //recive the names elements
System.out.println(element.text()); //prints "ZOLA (1)"
System.out.println(element.ownText()); // prints nothing
我的目标是只提取“ZOLA”,没有子节点的文本,但ownText
什么也不打印……我该怎么做?
解决方案
你可以使用这个:
String html = "<a href=\"/name/zola-1\">ZOLA <span class=\"tiny\">(1)</span></a>";
Document doc = Jsoup.parse(html);
Element elementA = doc.selectFirst("a");
System.out.println(elementA.ownText()); // ZOLA
推荐阅读
- redis - 限制 Redis 用户和/或命名空间存储大小
- php - 在所见即所得 Magento 2 中更改文件夹排序
- swift - 根据日期和链接数据过滤 fetchRequest
- microsoft-graph-api - Microsoft Graph 在驱动器搜索中返回相同的项目
- optimization - 如何找出在发布模式下对 Rust 代码应用了哪些优化?
- mysql-8.0 - 查询在 MySQL 5.6 中有效,但在 MySQL 8.0 中无效
- flutter - 当我应该有空值时 - Flutter
- angular - CKEDITOR 重复模块
- php - 使用 withCount() 计算所有文章
- mongodb - 无法读取未定义的属性“原型”-带有 typeorm 和 mongoDB 的 nestjs