簡體   English   中英

從Jsoup中的HTML文件提取文本信息

[英]Extracting text information from an HTML file in Jsoup

在我編寫的一段代碼中,我需要從網頁上獲取一些信息。 該信息將取決於登錄的用戶。

我正在嘗試在下面的代碼中獲得標記為nameid number的兩條信息。

<tr> 
  <td align="right"><b><label for="name" id="lblname">Name:</label></b> &nbsp;</td> 
  <td>*name here*</td> 
  <td align="right"><b><label for="ident" id="lblident">Local ID</label>:</b> &nbsp;</td> 
  <td>*id number here*</td> 
</tr> 

到目前為止,我能做到的最好的就是從下面的代碼中打印“元素”名稱。

Element name = information.getElementById("lblname");

它輸出的只是代碼的這一部分:

<label for="name" id="lblname">Name:</label>

我需要怎么做才能在HTML文件的此處定義名稱

在獲取<tr> </tr>后,您可能使用正則表達式提取所需內容

您可以使用Jsoup的CSS選擇器語法提取所需的確切元素,例如:

// select the "name" <td>
Element name = doc.select("td:has(label#lblname) + td").first();

// select the "id" <td>
Element id = doc.select("td:has(label#lblident) + td").first();

// print out the text 
System.out.println(name.text());
System.out.println(id.text());

使用您的示例,輸出將是:

*name here*
*id number here*

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM