在Jsoup中,我如何通过属性而不是数据来解析CSS查询



在J汤中,当我用CSS查询option解析URL http://www.singaporepools.com.sg/Lottery?page=wc_four_d时,我的第一个元素得到了"选择绘制日期",查询为<option selected="selected">

在我的代码中,如何让Jsoup返回"<option selected="selected">",而不是数据?

使用:

option[selected]

这意味着"每个具有selected属性的option元素"

这应该对您有效(参见下面的示例)。或者,如果有多个属性为selectedoption,则可以指定属性值:option[selected="selected"]

在CSS属性选择器中了解更多信息

J汤工作示例:

public static void main(String[] args) throws Exception {
    Document doc = Jsoup.connect("http://www.singaporepools.com.sg/Lottery?page=wc_four_d").get();
    Elements content = doc.select("option[selected]");
    System.out.println(content);
}

输出:

<option selected="selected">Select a draw date</option>

最新更新