【发布时间】:2013-04-09 10:49:27
【问题描述】:
我正在尝试解析:[www.neiu.edu/~neiutemp/PhoneBook/alpha.htm] 使用 TFHPPLE 解析器,我正在从表中的每个 TR(行)中查找第一个 TD(第一列) .这里 TD 的所有属性都是相同的。我无法区分 TD。
我能够获取所有 HTML 代码,但无法从每个 TR 中获取第一个 TD。在// 3(在代码中)tutorialsNodes 之后是空的。的输出
NSLog(@"Nodes are : %@",[tutorialsNodes description]);
是
练习1[62351:c07] 节点是:()。我看不出有什么问题。任何帮助,将不胜感激。 我解析此 URL 的代码:
NSURL *tutorialsUrl = [NSURL URLWithString:@"http://www.neiu.edu/~neiutemp/PhoneBook/alpha.htm"];
NSData *tutorialsHtmlData = [NSData dataWithContentsOfURL:tutorialsUrl];
// 2
TFHpple *tutorialsParser = [TFHpple hppleWithHTMLData:tutorialsHtmlData];
// 3
NSString *tutorialsXpathQueryString = @"//TR/TD";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
NSLog(@"Nodes are : %@",[tutorialsNodes description]);
// 4
NSMutableArray *newTutorials = [[NSMutableArray alloc] initWithCapacity:0];
for (TFHppleElement *element in tutorialsNodes) {
// 5
Tutorial *tutorial = [[Tutorial alloc] init];
[newTutorials addObject:tutorial];
// 6
tutorial.title = [[element firstChild] content];
// 7
tutorial.url = [element objectForKey:@"href"];
NSLog(@"title is: %@",[tutorial.title description]);
}
// 8
_objects = newTutorials;
[self.tableView reloadData];
【问题讨论】:
-
Xcode 不解析 HTML。这是针对 OS X os iOS 的吗?您应该相应地改写为“使用 Cocoa/Cocoa Touch”。
-
我已经重写了问题。
标签: ios objective-c html-parsing