简体   繁体   English

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

[英]In Jsoup, how do I parse the CSS query by the attribute, instead of the data?

In Jsoup when I parse the URL http://www.singaporepools.com.sg/Lottery?page=wc_four_d with the CSS Query option , I get "Select a draw date" for my first element, with the query of <option selected="selected"> . 在Jsoup中,当我使用CSS Query option解析URL http://www.singaporepools.com.sg/Lottery?page=wc_four_d时,我为第一个元素获得了“选择绘制日期” ,并通过<option selected="selected">进行了查询<option selected="selected">

In my code, how do I get Jsoup to return that " <option selected="selected"> ", instead of the data? 在我的代码中,如何让Jsoup返回“ <option selected="selected"> ”而不是数据?

Use: 采用:

option[selected]

It means "every option element that has a selected attribute" . 这表示“具有selected属性的每个option元素”

That should work for you (see example below). 那应该为您工作(请参阅下面的示例)。 Alternatively, if there were more than one option with the attribute selected , you could specify the attribute value: option[selected="selected"] . 或者,如果有多个option处于selected ,则可以指定属性值: option[selected="selected"]

Learn more in CSS Attribute Selectors . CSS属性选择器中了解更多信息。

Jsoup working example: Jsoup的工作示例:

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);
}

Output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM