简体   繁体   English

JSoup如何解析表3行

[英]JSoup how to parse table 3 rows

I have a table like this that i want to Parse to get the data-code value of row.id and the second and third column of the table. 我有一个这样的表,我想要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>

I want to print out. 我想要打印出来。

100, 18, John
200, 21, Mark

I have tried the following suggestion from this thread but its not selecting anything how to parse a table from HTML using jsoup 我从这个线程尝试了以下建议,但它没有选择任何如何使用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());
}

EDIT: also tried using Jsoup.connect() instead of parse() 编辑:也尝试使用Jsoup.connect()而不是parse()

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

Try like this: 试试这样:

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());
}

From documentation: 来自文档:

public Elements select(String cssQuery) Find elements that match the Selector CSS query, with this element as the starting context. public Elements select(String cssQuery)查找与Selector CSS查询匹配的元素,并将此元素作为起始上下文。 Matched elements may include this element, or any of its children. 匹配元素可以包括该元素或其任何子元素。 This method is generally more powerful to use than the DOM-type getElementBy* methods, because multiple filters can be combined, eg: •el.select("a[href]") - finds links (a tags with href attributes) •el.select("a[href*=example.com]") - finds links pointing to example.com (loosely) 此方法通常比DOM类型的getElementBy *方法更强大,因为可以组合多个过滤器,例如:•el.select(“a [href]”) - 查找链接(带有href属性的标记)•el .select(“a [href * = example.com]”) - 找到指向example.com的链接(松散地)

See the query syntax documentation in Selector. 请参阅Selector中的查询语法文档。

Parameters: cssQuery - a Selector CSS-like query Returns: elements that match the query (empty if none match) 参数:cssQuery - 一个类似Selector CSS的查询返回:与查询匹配的元素(如果没有匹配则为空)

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

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