JSoup使用循环获取多个值

时间:2016-01-08 22:02:14

标签: android jsoup

我正在使用JSoup尝试获取一个网站的几个值,这个网站只有1个TBody标签“luckely”并且是这样构建的:

dataquery.save(update_fields=data.keys())

我需要获得所有年(2015),月(11月),金额(€15,90)链接(a href =) 将值添加到列表视图中。

我已经有了一些代码,但不知怎的,我被困在获得金额值。 我还想稍后使用“链接”值来下载更多内容。

有人可以看看,请指导我一点吗? 谢谢。

<tbody>
 <tr>
   <td>2015</td>
   <td>November</td>
   <td class="no-border-left"></td>
   <td class="no-border-left">&euro; 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">&euro; 16,20</td>
   <td>
   <a href="/Invoice/Download?invoiceNo=2445473" target="_blank"><img alt="" src="/Content/Images/pdf_icon.png" /></a> 
   </td>   
 </tr>
       ....
</tbody>

2 个答案:

答案 0 :(得分:0)

好的,我设法解决了这个问题。 如果有人有同样的问题,这是工作代码:

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

答案 1 :(得分:0)

现在回答有点迟,但我会这样做:

Document doc = Jsoup.parse(myHtml);

Elements tableRows = doc.select("tbody > tr"); //get all the tr elements in the table

for (Element tableRow : tableRows) { //iterate over all the table rows (tr elements)
    String year = tableRow.child(0).text() //get the first td in the row, with the year, and get the text.
    String month = tableRow.child(1).text();
    String price = tableRow.child(3).text()
    String link = tableRow.children().select("a").first().attr("href"); //get the link (a), and then get the href attribute. You can also use abs:href to get an absolute url
}