简体   繁体   中英

What exception does jsoup.element.select() throw?

I wanted to execute the following code (Jsoup):

    Elements lessondata = td.select(" > table.container > tbody > tr > td > span.nobr");
    for (Element lesson : lessondata) {
    System.out.println("Lesson...");
        }

But only if it is possible to select this > table.container > tbody> tr > td > span.nobr. Sometimes, the website doesn't have these tags. Therefore, I wanted to put this piece of code in a 'try' piece, and catch the exception if the selection I want to make is impossible. But now I need to know: what exception does element.select() throw if it is impossible to select the given path?

I would appreciate your help.

You don't need to catch a Exception. According to the Documentation

@return elements that match the query (empty if none match)

just check if lessondata.isEmpty()

It doesn't throw any exception and returns an empty elements list.

Use isEmpty() or size() to check the returned list.

Elements lessondata = td.select(
                      " > table.container > tbody > tr > td > span.nobr");
if (!lessondata.isEmpty()) {
  for (Element lesson : lessondata) {
    System.out.println("Lesson...");
  }
}

JavaDoc: http://jsoup.org/apidocs/org/jsoup/nodes/Element.html#select(java.lang.String)

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