繁体   English   中英

JSoup如何解析表3行

[英]JSoup how to parse table 3 rows

我有一个这样的表,我想要Parse来获取row.id的数据代码值以及row.id的第二和第三列。

<table>
    <tr class="id" data-code="100">
       <td></td>
       <td>18</td>
       <td class="name">John</td>
    <tr/>
    <tr class="id" data-code="200">
       <td></td>
       <td>21</td>
       <td class="name">Mark</td>
    <tr/>
</table>

我想要打印出来。

100, 18, John
200, 21, Mark

我从这个线程尝试了以下建议,但它没有选择任何如何使用jsoup从HTML解析表

URL url = new URL("http://www.myurl.com");
Document doc = Jsoup.parse(url, 3000);

Element tables = doc.select("table[class=id]");

for(Element table : tables)
{
     System.out.println(table.toString());
}

编辑:也尝试使用Jsoup.connect()而不是parse()

Document doc = null;
try
{
    doc = Jsoup.connect("http://www.myurl.com").get();
} 
catch (IOException e) 
{
    e.printStackTrace();
}

试试这样:

URL url = new URL("http://www.myurl.com");
Document doc = Jsoup.parse(url, 3000);
// This should work now
Element tables = doc.select("table tr .id");
// This propably should work too
Element tables2 = doc.select("table tr[class*=id]");

for(Element table : tables)
{
     System.out.println(table.toString());
}

来自文档:

public Elements select(String cssQuery)查找与Selector CSS查询匹配的元素,并将此元素作为起始上下文。 匹配元素可以包括该元素或其任何子元素。 此方法通常比DOM类型的getElementBy *方法更强大,因为可以组合多个过滤器,例如:•el.select(“a [href]”) - 查找链接(带有href属性的标记)•el .select(“a [href * = example.com]”) - 找到指向example.com的链接(松散地)

请参阅Selector中的查询语法文档。

参数:cssQuery - 一个类似Selector CSS的查询返回:与查询匹配的元素(如果没有匹配则为空)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM