简体   繁体   English

检查表数据-Jsoup

[英]Check for table data - Jsoup

Is there a way to check if a table has a certain row using jsoup? 有没有一种方法可以使用jsoup检查表是否具有特定行?

I am getting an java.lang.IndexOutOfBoundsException: Invalid location 1, size is 1 exception, my code for getting the info out of the table is: 我收到一个java.lang.IndexOutOfBoundsException: Invalid location 1, size is 1异常,我从表中获取信息的代码是:

  docTide = Jsoup.connect("http://www.mhpa.co.uk/search-tide-times/").timeout(600000).get();
  Elements tideTimeOdd = docTide.select("div.tide_row.odd div:eq(0)");
  Elements tideTimeEven = docTide.select("div.tide_row.even div:eq(0)");
  Elements tideHightOdd = docTide.select("div.tide_row.odd div:eq(2)");
  Elements tideHightEven = docTide.select("div.tide_row.even div:eq(2)");
  Element firstTideTime = tideTimeOdd.first();
  Element secondTideTime = tideTimeEven.first();
  Element thirdTideTime = tideTimeOdd.get(1);
  Element fourthTideTime = tideTimeEven.get(1);

The exception is occurring because sometime the table only has 3 rows instead of 4, in this order; 发生异常是因为有时表仅具有3行而不是4行(按此顺序)。 odd even odd even 奇数偶数

it is the last 'even' row that is causing the problem. 导致问题的是最后“偶数”行。

  <div class="tide_row odd"> 
  <div class="time">00:57</div>
  <div class="height_m">4.9</div>
  <div class="height_f">16,1</div>
  <div class="range_m">1.9</div>
  <div class="range_f">6,3</div>
  </div>
  <div class="tide_row even">
  <div class="time">07:23</div>
  <div class="height_m">2.9</div>
  <div class="height_f">9,6</div>
  <div class="range_m">2</div>
  <div class="range_f">6,7</div>
  </div>
  <div class="tide_row odd">
  <div class="time">13:46</div>
  <div class="height_m">5.1</div>
  <div class="height_f">16,9</div>
  <div class="range_m">2.2</div>
  <div class="range_f">7,3</div>
  </div>
  <div class="tide_row even">
  <div class="time">20:23</div>
  <div class="height_m">2.8</div>
  <div class="height_f">9,2</div>
  <div class="range_m">2.3</div>
  <div class="range_f">7,7</div>
  </div>

To simply check the size of the Elements object, use the size() method to determine if it exists or not. 要简单地检查Elements对象的size() ,请使用size()方法确定它是否存在。

To check for a certain Element use the contains() method. 要检查某个Element使用contains()方法。

You might also consider using a loop to iterate over all the Element objects in your Elements collection. 您可能还考虑使用循环遍历Elements集合中的所有Element对象。


if(tideTimeEven.size() > 1)
    //Do something

You could do 你可以做

if (tideTimeEven.size() > 1) {
    Element fourthTideTime = tideTimeEven.get(1);
}

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

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