You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用JSoup从表格列的指定Class中提取文本?

解决Jsoup爬取指定类文本的问题

我来帮你排查代码不生效的原因啦~你的问题出在CSS选择器的写法错误上,咱们一步步修正:

问题分析

你写的选择器"tbody.td.auto-style162"逻辑有误:

  • .是CSS类选择器的标识,这个写法相当于在找一个标签为<tbody>,同时拥有td和auto-style162两个类的元素,但页面里实际是<td>标签带有auto-style162类,并且嵌套在<tbody>里。

修正后的代码

你需要用空格分隔后代元素,来指定“在<tbody>下的、带有auto-style162类的<td>”,或者直接匹配目标<td>:

写法一:直接匹配目标td(推荐,更简洁)

Elements para = doc.select("td.auto-style162");
for (Element e : para) {
    System.out.println(e.text());
}

写法二:精准限定在tbody下的td

如果页面中其他位置也有同名类的元素,可加上tbody限定范围:

Elements para = doc.select("tbody td.auto-style162");
for (Element e : para) {
    System.out.println(e.text());
}

额外小提示

如果页面中只有一个符合条件的元素,你可以直接获取第一个匹配项的文本,不用循环:

Element targetPara = doc.selectFirst("td.auto-style162");
if (targetPara != null) {
    System.out.println(targetPara.text());
}

内容的提问来源于stack exchange,提问作者Rajesh Bahl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 23:52:43