简体   繁体   English

JSoup从中提取值 <td> 标签

[英]JSoup extracting values from <td> tag

I'm trying to extract values from td tag, but it doesn't work, one of td contains value which is called "Technology", and I want to get td value which is after him, for example td is 'Technology' and td which is after that: 'Windows Server' <- how can I get this value? 我正在尝试从td标签中提取值,但是它不起作用,其中td包含称为“技术”的值,我想获取td他之后的td值,例如td为“技术”,之后是td :“ Windows Server” <-如何获得该值? here is a code: 这是一个代码:

</td>
   </tr>
      </tbody>
         </table>
       </dd>
      <dt>
      <a href="#course-info">Informacje o kursie</a></dt><dd id="course-info"><table><tbody>

<tr>
  <td>Odbiorcy:</td>
  <td style="text-transform: capitalize;">IT Professionals</td>
</tr>
<tr>
   <td>Technologia:</td><td>Windows Server</td>
</tr>
<tr>
   <td>Poziom:</td><td>300</td>
</tr>
<tr>
   <td>Bieżąca wersja:</td><td>B</td>
</tr>

when I'm doing something like that: 当我做这样的事情时:

for (Element element : doc.select("#course-info").first().children()){
        if(element.text().contains("Tech")){
            System.out.println(element.nextElementSibling().html());
        }
    }

Then NPE appears (element), but when I delete this loop, whole method works fine. 然后出现NPE(元素),但是当我删除此循环时,整个方法都可以正常工作。

The specific CSS selector for "Windows Server" is #course-info > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(2) . “ Windows Server”的特定CSS selector#course-info > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(2)
If you want to know which selector to use, open your browser's developers tools (F12), and inspect the DOM. 如果您想知道使用哪个选择器,请打开浏览器的开发人员工具(F12),然后检查DOM。 Select the desired element and get the selector. 选择所需的元素并获取选择器。
I believe that you have more rows in your table, so you'll have to loop thru. 我相信您的表中有更多的行,因此您必须循环遍历。 the table, by changing one (or more) of the number constants in the above selector. 通过更改上述选择器中的一个(或多个)数字常量来创建表格。

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

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