Jsoup获得了孩子们的班级名称

时间:2017-12-12 16:35:20

标签: java jsoup

我是Jsoup的新手,我不知道如何提取以下内容。我有一个元素,有几个跨度我想要达到。

<div class="first inlineblock">
 <span class="float_lang_base_1">Prev. Close</span>
 <span class="float_lang_base_2 bold">175</span>
</div>
<div class="inlineblock">
 <span class="float_lang_base_1">Day's Range</span>
 <span class="float_lang_base_2 bold">173.35 - 176.9</span>
</div>
<div class="inlineblock">
 <span class="float_lang_base_1">Revenue</span>
 <span class="float_lang_base_2 bold">21.07B</span>
</div>
<div class="first inlineblock">
 <span class="float_lang_base_1">Open</span>
 <span class="float_lang_base_2 bold">174.45</span>
</div>

我的代码如下:

Element OverviewHeadline = doc1.select("div[class=\"clear overviewDataTable\"]").first();
        System.out.println("OverviewHeadline = " + OverviewHeadline.html());

        Elements elements = OverviewHeadline.children();            

        for (Element e : elements)
        {
            System.out.println("e = " + e.html());

            if (e.child(0).className() == "float_lang_base_1")
            {
                if (e.child(0).text() == "Prev. Close")
                {
                    sPreviousClose = e.child(1).text();
                }   
            }

            if (e.child(0).className() == "float_lang_base_1")
            {
                if (e.child(0).text() == "Day's Range")
                {
                    sDaysRange = e.child(1).text();
                    iLen = sDaysRange.length();
                    ipos = sDaysRange.indexOf(" - ");
                    sMarketLow = sDaysRange.substring(0, ipos);                     
                    sMarketHigh = sDaysRange.substring(ipos + 3, iLen - (ipos + 3)  );
                }   
            }

            if (e.child(0).className() == "float_lang_base_1")
            {
                if (e.child(0).text() == "Open")
                {
                    sOpen = e.child(1).text();
                }   
            }               
        }

我不确定如何获取类名。

1 个答案:

答案 0 :(得分:1)

我明白了。以下是我如何使用它。

Element OverviewHeadline = doc1.select("div[class=\"clear overviewDataTable\"]").first();

        Elements elements = OverviewHeadline.children();            

        for (Element e : elements)
        {
            String sBase1 = e.select("span[class=\"float_lang_base_1\"]").text();
            String sBase2 = e.select("span[class=\"float_lang_base_2 bold\"]").text();

            if (sBase1.equals("Prev. Close"))
            {
                sPreviousClose = sBase2;
            }               

            if (sBase1.equals("Day's Range"))
            {
                sDaysRange = sBase2;
            }

            if (sBase1.equals("Open"))
            {
                sOpen = sBase2;
            }   
        }

在选择OverviewHeadLine的孩子时,它让我进入spans;我使用sBase1sBase2进行选择。然后我可以确定我需要的物品。 sBase1字段名称sBase2