我如何在HTML中解析表



我正在尝试用大量表分析HTML页面。我已经搜索了如何用目标C解析HTML的网络,我找到了Hpple。我会寻找一个导致我到达的教程:

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

在本教程中,我试图解析一些论坛新闻,这些新闻有很多桌子(希伯来语):新闻论坛

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

我最新尝试的代码是:

 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中,并通过 - string by stringbyevaluatingjavascriptfromstring获取内容,/p>

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

最新更新