如何在HTML中解析表?
我正在尝试解析一个包含很多表的HTML页面。我在网上搜索了如何用Objective C解析HTML,找到了hpple。我想找一个教程,让我了解: 在本教程中,我尝试解析一些论坛新闻,其中有许多来自此站点的表格(希伯来文): 我试图解析新闻标题,但我不知道在代码中写什么。每次我试图到达我得到的路径,“节点为零。” 我最近一次尝试的代码是:如何在HTML中解析表?,html,objective-c,xcode,parsing,hpple,Html,Objective C,Xcode,Parsing,Hpple,我正在尝试解析一个包含很多表的HTML页面。我在网上搜索了如何用Objective C解析HTML,找到了hpple。我想找一个教程,让我了解: 在本教程中,我尝试解析一些论坛新闻,其中有许多来自此站点的表格(希伯来文): 我试图解析新闻标题,但我不知道在代码中写什么。每次我试图到达我得到的路径,“节点为零。” 我最近一次尝试的代码是: NSURL *contributorsUrl = [NSURL URLWithString:@"http://rotter.net/cgi-bin/list
NSURL *contributorsUrl = [NSURL URLWithString:@"http://rotter.net/cgi-bin/listforum.pl"];
NSData *contributorsHtmlData = [NSData dataWithContentsOfURL:contributorsUrl];
// 2
TFHpple *contributorsParser = [TFHpple hppleWithHTMLData:contributorsHtmlData];
// 3
NSString *contributorsXpathQueryString = @"//body/div/center/center/table[@cellspacing=0]/tbody/tr/td/table[@cellspacing=1]/tbody/tr[@bgcolor='#FDFDFD']/td[@align='right']/font[@class='text15bn']/font[@face='Arial']/a/b";
NSArray *contributorsNodes = [contributorsParser searchWithXPathQuery:contributorsXpathQueryString];
// 4
NSMutableArray *newContributors = [[NSMutableArray alloc] initWithCapacity:0];
for (TFHppleElement *element in contributorsNodes) {
// 5
Contributor *contributor = [[Contributor alloc] init];
[newContributors addObject:contributor];
// 6
有人能指导我获取标题吗?不确定这是否是您的选择,但如果所需的表具有唯一id,您可以使用一种混乱的方法:将html加载到UIWebView中,并通过以下方式获取内容:
// desired table container's id is "msg"
NSString* value = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('msg').innerHTML"];