简体   繁体   中英

How to select Element with empty class using Jsoup

I want to select the element with class="" like

<li class="" > </li>

I used

Elements topProductSecNav = topNavWrapper.select("li[class=]");

but I got java.lang.IllegalArgumentException: String must not be empty exception.

Use this: Elements topProductSecNav=topNavWrapper.select(li[class=\\"\\"]");
See working example here .

I would use the regex css selector li[class~=^$] :

String html= "<li class=\"\" > </li>"
           + "<li class= > </li>"
           + "<li class > </li>"
           + "<li > </li>"
           + "<li class=\"test\" > </li>";
Document doc = Jsoup.parse(html,"");

Elements liWithClassButNoName = doc.select("li[class~=^$]");

for (Element li:liWithClassButNoName){
    System.out.println("li = "+ li);
}

results in this output (only the first 3 lis match):

li = <li class=""> </li>
li = <li class> </li>
li = <li class> </li>

Explanation:

~= means regex and ^$ searches for an empty string

Jsoup will delete the = in the second example li element <li class= > . The regular expression also matches aparently a non existing string, so if you need to filter these out you may want to go with the solution given by @TDG.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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