简体   繁体   English

jsoup.element.select()抛出什么异常?

[英]What exception does jsoup.element.select() throw?

I wanted to execute the following code (Jsoup): 我想执行以下代码(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. 但只有可以选择这个> 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. 因此,我想把这段代码放在'try'中,如果我想做的选择是不可能的,那就抓住异常。 But now I need to know: what exception does element.select() throw if it is impossible to select the given path? 但现在我需要知道:如果无法选择给定的路径,element.select()会抛出什么异常?

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() 只检查lessondata.isEmpty()

It doesn't throw any exception and returns an empty elements list. 它不会抛出任何异常并返回一个空元素列表。

Use isEmpty() or size() to check the returned list. 使用isEmpty()size()来检查返回的列表。

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) JavaDoc: http//jsoup.org/apidocs/org/jsoup/nodes/Element.html#select(java.lang.String)

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

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