我正在使用JSoup尝试获取一个网站的多个值,该网站仅“幸运地”只有1个TBody标签,并且这样构建:
<tbody>
<tr>
<td>2015</td>
<td>November</td>
<td class="no-border-left"></td>
<td class="no-border-left">€ 15,90</td>
<td>
<a href="/Invoice/Download?invoiceNo=2632992" target="_blank"><img alt="" src="/Content/Images/pdf_icon.png" /></a> </td>
</tr>
<tr>
<td>2015</td>
<td>Oktober</td>
<td class="no-border-left"></td>
<td class="no-border-left">€ 16,20</td>
<td>
<a href="/Invoice/Download?invoiceNo=2445473" target="_blank"><img alt="" src="/Content/Images/pdf_icon.png" /></a>
</td>
</tr>
....
</tbody>
我需要将所有年份(2015年),月份(11月),金额(€15,90)和链接(a href =) 值通过循环添加到列表视图中。
我已经有了一些代码,但是不知何故我一直坚持获取金额值。我还想稍后使用“链接”值来下载更多内容。
有人可以看一下,请指导我一点吗?谢谢你。
.... Elements Tbody = doc.select("TBODY"); for (Element p : Tbody) { Iterator<Element> postIt = p.select("td").iterator(); String YeaR = postIt.next().text(); String MontH = postIt.next().text(); postIt.next(); postIt.next(); Element amount = doc.select("td.no-border-left").first(); String amounT = amount.text(); Element hrefs = doc.select("a[href]").first(); String linK = hrefs.text(); } ....
好的,我设法解决了这个问题。如果有人遇到相同的问题,请参见以下工作代码:
try {
CharSequence cs1 = "€";
is = getActivity().getAssets().open("test.htm");
Document doc = Jsoup.parse(is, "UTF-8", "http://example.com/");
Elements rows = doc.select("tr");
for (int i = 1; i < rows.size(); i++) {
Element row = rows.get(i);
Elements cols = row.select("td");
Elements links = row.getElementsByTag("a");
String YeaR = cols.get(0).text();
//Log.e("JSOUP: ", YeaR);
String MontH = cols.get(1).text();
//Log.e("JSOUP: ", MontH);
for (Element tes : cols)
if (tes.text().contains(cs1)) {
String amounT = tes.text();
//Log.e("JSOUP: ", amounT);
}
for (Element link : links) {
String url = link.attr("href");
//Log.e("JSOUP: ", url);
}
}
if (is != null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
它给出了输出:
E/JSOUP:: 2015
E/JSOUP:: November
E/JSOUP:: € 15,90
E/JSOUP:: /Invoice/Download?invoiceNo=2632992
E/JSOUP:: 2015
E/JSOUP:: Oktober
E/JSOUP:: € 16,20
E/JSOUP:: /Invoice/Download?invoiceNo=2445473
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句