Java 使用jsoup从表的第一列获取数据

Java 使用jsoup从表的第一列获取数据,java,html,html-table,jsoup,html-parsing,Java,Html,Html Table,Jsoup,Html Parsing,为了回答我的问题,我创建了一个简单的HTML页面,其摘录如下: //In reality, it should be connect(html).get(). //Also, suppose that the String `html` has the full source code. Document doc = Jsoup.parse(html); Elements table = doc.select("table.fruit-vegetables").select("tbody"

为了回答我的问题,我创建了一个简单的HTML页面,其摘录如下:

//In reality, it should be connect(html).get(). 
//Also, suppose that the String `html` has the full source code.
Document doc = Jsoup.parse(html); 

Elements table = doc.select("table.fruit-vegetables").select("tbody").select("tr").select("td").select("a");

for(Element element : table){
    System.out.println(element.text());
}

果
蔬菜
我想使用Jsoup从第一列“Fruit”中提取数据。因此,结果应该是:

Apples
Oranges
我编写了一个程序,其摘录如下:

//In reality, it should be connect(html).get(). 
//Also, suppose that the String `html` has the full source code.
Document doc = Jsoup.parse(html); 

Elements table = doc.select("table.fruit-vegetables").select("tbody").select("tr").select("td").select("a");

for(Element element : table){
    System.out.println(element.text());
}
该计划的结果是:

Apples
Carrots
Oranges
Peas

我知道有些事情做得不好,但我找不到我的错误。堆栈溢出中的所有其他问题都没有解决我的问题。我该怎么办?

你似乎在找我

Elements el = doc.select("table.fruit-vegetables td:eq(0)");
for (Element e : el){
    System.out.println(e.text());
}
从中可以找到
:eq(n)
的说明

:eq(n)
:查找同级索引等于
n
的元素;e、 g.
表单输入:等式(1)


因此,使用
td:eq(0)
我们选择每个
都是其父代的第一个子代-在这种情况下,您似乎正在寻找

Elements el = doc.select("table.fruit-vegetables td:eq(0)");
for (Element e : el){
    System.out.println(e.text());
}
从中可以找到
:eq(n)
的说明

:eq(n)
:查找同级索引等于
n
的元素;e、 g.
表单输入:等式(1)


因此,使用
td:eq(0)
我们选择每个
都是其父代的第一个孩子-在这种情况下

您的回答帮助我解决了很多问题。非常感谢。你的回答对我解决问题帮助很大。非常感谢你。