简体   繁体   English

JSOUP:获取具有特定行距的td

[英]JSOUP: Getting a td with a specific rowspan

I've just recently picked up Jsoup to aid me in a personal project of mine. 我最近才选择Jsoup来帮助我进行我的个人项目。 Right now i'm trying to parse specific data from this table but i'm not exactly sure how to go about doing that. 现在,我正在尝试从该表中解析特定数据,但我不确定该如何去做。 Basically i'm trying to extract the text from the td with rowspan="2" but I can't seem to figure out the right select statement to use. 基本上,我正在尝试使用rowspan =“ 2”从td中提取文本,但是我似乎无法弄清楚要使用的正确选择语句。 Any help would be appreciated, thanks! 任何帮助,将不胜感激,谢谢!

 <table style="background-color:inherit;line-height:100%;"> <tbody> <tr> <td rowspan="2" style="font-size:16px;width:30px;">16</td> <td style="padding:0px;font-size:8px;line-height:8px;"><span style="color:#ff0000;">11</span> </td> </tr> <tr> <td style="padding:0px;font-size:8px;line-height:8px;"><span style="color:#0000ff;">5</span> </td> </tr> </tbody> </table> 

Use attribute selector like following. 如下使用属性选择器。

 alert( $('td[rowspan="2"]').text()) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table style="background-color:inherit;line-height:100%;"> <tbody> <tr> <td rowspan="2" style="font-size:16px;width:30px;">16</td> <td style="padding:0px;font-size:8px;line-height:8px;"><span style="color:#ff0000;">11</span> </td> </tr> <tr> <td style="padding:0px;font-size:8px;line-height:8px;"><span style="color:#0000ff;">5</span> </td> </tr> </tbody> </table> 

You can do this. 你可以这样做。

Elements rowspans = doc.select("td[rowspan=2]");
String text = rowspans.text();

Result will be a single string. 结果将是一个字符串。 But if you have multiple rowspan=2's, it might be better to store the data as elements in an array: 但是,如果您有多个rowpan = 2,则最好将数据作为元素存储在数组中:

ArrayList<String> array = new ArrayList<String>();
for (Element rowspan : rowspans) {
    array.add(rowspan.text());
}

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

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