JSOUP从<select name =“...”> </select>中提取数据

时间:2014-12-25 06:36:01

标签: java html select jsoup

这是一个HTML代码。我想打印“颜色:”和各种颜色选项。不知怎的,我想通过使用“select name = att1”来表示选择的名称标签。

<div class="box-body">

    <div id="attributeInputs" class="attribute-inputs" data-defcolor="Palm">

        <div class="row thinpad-top att1row">
            <div class="small-24 columns">
                <label for="att1_BA0FEDC6-8BF1-11E4-B816-87E377679EE2">Color:</label>
            </div>
            <div class="small-24 columns">
                <select name="att1" id="att1_BA0FEDC6-8BF1-11E4-B816-87E377679EE2">
                    <option value="">Please Select Color</option>
                    <option value="Black">Black</option>
                    <option value="Palm">Palm</option>
                </select>
            </div>

我尝试了很多jsoup标签。但我无法获得所需的输出

我想输出这样的东西:

Please Select Color:
Black 

Palm

请帮助

1 个答案:

答案 0 :(得分:0)

此代码将提取select标记和选项标记

中的元素
 String html="<div class=\"box-body\">\n" +
                        "\n" +
                        "    <div id=\"attributeInputs\" class=\"attribute-inputs\" data-defcolor=\"Palm\">\n" +
                        "\n" +
                        "        <div class=\"row thinpad-top att1row\">\n" +
                        "            <div class=\"small-24 columns\">\n" +
                        "                <label for=\"att1_BA0FEDC6-8BF1-11E4-B816-87E377679EE2\">Color:</label>\n" +
                        "            </div>\n" +
                        "            <div class=\"small-24 columns\">\n" +
                        "                <select name=\"att1\" id=\"att1_BA0FEDC6-8BF1-11E4-B816-87E377679EE2\">\n" +
                        "                    <option value=\"\">Please Select Color</option>\n" +
                        "                    <option value=\"Black\">Black</option>\n" +
                        "                    <option value=\"Palm\">Palm</option>\n" +
                        "                </select>\n" +
                        "            </div>";

                Document doc = Jsoup.parse(html);
                Elements links = doc.select("select option");
                for (Element link : links) {
                    String linkText = link.text();
                    System.out.println(linkText);
                }