有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我需要通过jsoup解析一个页面。该页面包含带有标签divh3a等的元素。我想分析这些元素并选择a(即标题)以在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中单独显示每个标题,如下所示:

THIS IS example 1
lead by this example
showed example for the people
we set example for people

共 (2) 个答案

  1. # 1 楼答案

    当前,您将刮取的数据分配给循环中的title,然后将title分配给jlist的循环之外的。因此,循环完成后title的值始终是最后一个值

    替换这个

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

    用这个

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

    实际上,你不会每次都添加标题。循环用找到的新值替换每个时间标题,然后在循环后将其添加到列表中。像这样的事情可能会按照你想要的方式进行:

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