我正在使用 hpple 插件在我的 iphone 应用程序中解析和显示 html 元素。我遇到的问题是说我要解析的网页上有一个表格,有几行(可能会不时改变)。如何获取此表中的行数,并遍历每一行并获取每一行的不同文本内容。hpple插件可以做到这一点吗?
提前致谢。
如果只想用“tableClass”类计算表中的行,那么你可以尝试这样的事情
// Count the number of rows in a particular table
NSString *searchString = [NSString stringWithFormat:@"//table[@class='tableClass']/tr;
NSArray *tableRows = [xpathParser search:searchString];
NSInteger rows = [tableRows count];
NSLog(@"There are %d table Rows", rows);
// For loop to step through the rows
for(int j = 1; j <= rows; j++) {
searchString = [NSString stringWithFormat:@"//table[@class='tableClass']/tr[%d]/td", j];
NSArray *tableCells = [xpathParser search:searchString];
}
这应该逐行遍历表格,并且每次都刮取单个数据单元格(我没有对其进行测试,因此无法保证。)这样做的问题是,如果您的表格有多个,它将非常慢行。
你最好只调用表格并从表格中一次刮掉所有的 td 单元格,然后决定它们如何组成行。如果一行中的单元格数量保持不变,这种方法很容易。