我试图解析HTML以获取特定数据,但是在换行符方面遇到了问题,至少我认为这就是问题所在。我使用简单的子字符串方法来分解HTML,因为我预先知道我要查找什么。
以下是我的解析方法:
public static void parse(String response, String[] hashItem, String[][] startEnd) throws Exception
{
for (i = 0; i < hashItem.length; i++)
{
part = response.substring(response.indexOf(startEnd[i][0]) + startEnd[i][0].length());
value = part.substring(0, part.indexOf(startEnd[i][1]));
DATABASE.setHash(hashItem[i], value);
}
}
这是一个让我困扰的HTML示例。
<table cellspacing=0 cellpadding=2 class=smallfont>
<tr onclick="lu();" onmouseover="style.cursor='hand'">
<td class=bodybox nowrap> 21,773,177,147 $ </td><td></td>
<td class=bodybox nowrap> 629,991,926 F </td><td></td>
<td class=bodybox nowrap> 24,537 P </td><td></td>
<td class=bodybox nowrap> 0 T </td>
<td></td><td class=bodybox nowrap> RT </td>
文本中存在隐藏的回车字符,但当我尝试将它们添加到我正在使用的字符串中时,效果并不好,甚至根本无法工作。是否有一种方法或更好的方法来从HTML中剥离隐藏字符,使其更容易解析?非常感谢您的帮助。
tdText.replace(Jsoup.parse(" ").text(), " ");
- Michael Mrozek