简体   繁体   中英

Jsoup: “Select only links with text() equal to”

One way to cul links with text() that equals some predefined strings is straightforward:

Elements links = document.getElementsByTag("a");               
for (Element link : links) {
    if (link.text().equals("So & so") || link.text().equals("such & such") {
        // add link.attr("href") to our container;
    }
}                   

But as the number of text() conditions grows, this approach looks less and less efficient.

Is there a better way to accomplish this in Jsoup?

This has nothing specific to do with Jsoup, but why not use a Set such as a HashSet to hold your valid Strings? Then if the set were called "validTextSet", you could quite simply and efficiently test if the text is in the set with

     if (validTextSet.contains(link.text())) {
        // add link.attr("href") to our container;
     }

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