选择并迭代具有相同名称的元素和子元素(Jsoup)

时间:2018-01-12 14:22:56

标签: java html jsoup

我需要通过jsoup解析页面。该页面包含带有标记divh3a等的元素。我想解析这些元素并选择a(即标题)以显示在{{1 }}。

例如,页面如下所示:

jList

这是代码:

<div class="start">
    <div class="g">
        <div class="abc">
            <a class="picture" href="www.img.com"><img src="img" alt="image1"></a>
            <div class="xyz">
                <h3 class="_r">
                    <a class="title" href="www.example.com" onmousedown="return rwt(this,'','','','1','adf','','ahahh','','',event)">THIS IS <em>example</em>1</a>
                </h3>
            </div>
        </div>
    </div>

    <div class="g">
        <div class="abc">
            <a class="picture" href="www.img.com"><img src="img" alt="image2"></a>
            <div class="xyz">
                <h3 class="_r">
                    <a class="title" href="www.example.com" onmousedown="return rwt(this,'','','','1','adf','','ahahh','','',event)">lead by this<em>example</em></a>
                </h3>
            </div>
        </div>
    </div>

    <div class="g">
        <div class="abc">
            <a class="picture" href="www.img.com"><img src="img" alt="image3"></a>
            <div class="xyz">
                <h3 class="_r">
                    <a class="title" href="www.example.com" onmousedown="return rwt(this,'','','','1','adf','','ahahh','','',event)">showed<em>example</em>for the people</a>
                </h3>
            </div>
        </div>
    </div>

    <div class="g">
        <div class="abc">
            <a class="picture" href="www.img.com"><img src="img" alt="image4"></a>
            <div class="xyz">
                <h3 class="_r">
                    <a class="title" href="www.example.com" onmousedown="return rwt(this,'','','','1','adf','','ahahh','','',event)">we set<em>example</em>for people</a>
                </h3>
            </div>
        </div>
    </div>
</div>

我得到的输出是最后一个元素String url = "http://www.google.com/search?q=example&tbm=nws&source=lnms"; String title = ""; try { Document doc = Jsoup.connect(url).userAgent("Chrome").timeout(5000).get(); Elements e = doc.select("div.g"); for (Element e1 : e) { title = e1.getElementsByTag("a").text(); } DefaultListModel<String> listModel = new DefaultListModel<>(); listModel.addElement(title); jList.setModel(listModel); } catch (IOException ex) { Logger.getLogger(MainUI.class.getName()).log(Level.SEVERE, null, ex); } 的标题:

div.g

我想从每个we set example for people中选择标题,并在div.g中分别显示每个标题,如下所示:

jList

2 个答案:

答案 0 :(得分:1)

目前,您将已删除的数据分配到循环中的 //div/p/text() ,然后外部将您分配title的循环分配给jlist。因此,循环完成后title的值将始终是最后一个值。

替换此......

title

有了......

for (Element e1 : e) {
    title = e1.getElementsByTag("a").text();
}
DefaultListModel<String> listModel = new DefaultListModel<>();
listModel.addElement(title);

答案 1 :(得分:1)

你实际上每次都不会添加标题。循环将每次标题替换为找到的新值,并在循环之后将其添加到列表中。这样的事情可能会按你想要的方式运作:

    DefaultListModel<String> listModel = new DefaultListModel<>();  
    for (Element e1 : e) {
       listModel.addElement(e1.getElementsByTag("a").text());
    }