簡體   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