如何解析HTML中的表格?

时间:2022-09-04 08:19:11

I'm trying to parse an HTML page with a lot of tables. I've searched the net on how to parse HTML with Objective C and I found hpple. I'd look for a tutorial which lead me to:

我正在尝试用很多表来解析HTML页面。我在网上搜索了如何用Objective C解析HTML,我发现了hpple。我会找一个教程,引导我:

http://www.raywenderlich.com/14172/how-to-parse-html-on-ios

With this tutorial I tried to parse some forum news which has a lot of tables from this site (Hebrew): news forum

在本教程中,我尝试解析一些论坛新闻,其中包含来自此站点(希伯来语)的很多表:新闻论坛

I tried to parse the news title, but I don't know what to write in my code. Every time I try to reach the path I get, "Nodes was nil."

我试图解析新闻标题,但我不知道在我的代码中写什么。每次我试图达到我得到的路径时,“节点都是零。”

The code of my latest attempt is:

我最近的尝试的代码是:

 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

Could somebody guide me through to getting the titles?

有人可以指导我获得头衔吗?

1 个解决方案

#1


0  

Not sure if that's the option for you, but if desired table have unique id's you could use a messy approach: load that html into UIWebView and get contents via – stringByEvaluatingJavaScriptFromString: like this:

不确定这是否适合您,但如果需要表有唯一ID,您可以使用凌乱的方法:将html加载到UIWebView并通过 - stringByEvaluatingJavaScriptFromString获取内容:像这样:

// desired table container's id is "msg"
NSString* value = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('msg').innerHTML"];

#1


0  

Not sure if that's the option for you, but if desired table have unique id's you could use a messy approach: load that html into UIWebView and get contents via – stringByEvaluatingJavaScriptFromString: like this:

不确定这是否适合您,但如果需要表有唯一ID,您可以使用凌乱的方法:将html加载到UIWebView并通过 - stringByEvaluatingJavaScriptFromString获取内容:像这样:

// desired table container's id is "msg"
NSString* value = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('msg').innerHTML"];