使用JSOUP获取图像标题

时间:2015-05-21 14:12:18

标签: android parsing jsoup

我有这张桌子。

<div id="activeArrivi">
    <div class="aggBox">
        <label>Ultimo aggiornamento:</label> <span class="update">21/05/2015 15:25</span>
    </div>

    <table>
        <thead>
        <tr>
            <th>Compagnia</th>
            <th>n.</th>
            <th>Provenienza</th>
            <th>Schedulato</th>
            <th>Stimato</th>
            <th>Stato</th>
        </tr>
        </thead>
        <tbody>
                        <tr id="a0" style="background-color: rgba(253, 253, 253, 0.8);">
                <td>
                    <img class="company" alt="RYANAIR" src="/img/RYANAIR.png" original-title="RYANAIR">                 </td>
                <td>05021</td>
                <td>Roma Fiumicino</td>
                <td>21/05/2015 14                       :30</td>
                <td>21/05/2015 14                       :45</td>
                <td>
                                            <img src="/images/volo_green.gif" alt="Atterrato" title="Atterrato"><br> Atterrato                  </td>
            </tr>
                        </tbody>
    </table>
</div>

这里有我的代码:

  doc = Jsoup.connect("http:/url").timeout(10*1000).get();


            Element table = doc.select("table").first();
            Iterator<Element> iterator = table.select("td").iterator();

            //iterator.next(); // first one is image, skip it

            while(iterator.hasNext()){
                arrivi++;

                A_Compagnia[arrivi] = iterator.next().attr("alt");
                A_CodiceVolo[arrivi] = iterator.next().text();
                A_Citta[arrivi] = iterator.next().text();
                A_OraPrevista[arrivi] = iterator.next().text();
                A_OraStimata[arrivi] = iterator.next().text();
                A_StatoVolo[arrivi] = iterator.next().text();

            }

我可以获取所有数据...但我不知道如何获取标签中的文本:alt =“RYANAIR”并将文本放在此处:A_Compagnia [1]; 有人能够解决我的问题!? 谢谢你们

2 个答案:

答案 0 :(得分:0)

尝试这样的事情。

ArrayList<String> titles = new ArrayList<String>();     
doc = Jsoup.connect("http:/url").timeout(10*1000).get(); 
     Element table = doc.select("table").first();
     for (Element row : table.select("td")) {
                        Elements column = row.select("img");
                        String title = column.attr("alt");
                        titles.add(title);
                        Log.i("All Titles",titles.toString());
                    }

答案 1 :(得分:0)

试试这个Elements img = doc.select("img"); String title = link.attr("alt");。让我知道进展