【问题标题】:How can I parse tables in HTML?如何解析 HTML 中的表格?
【发布时间】:2012-11-04 17:25:08
【问题描述】:

我正在尝试解析包含大量表格的 HTML 页面。我在网上搜索了如何使用 Objective C 解析 HTML 并找到了 hpple。我会寻找一个引导我的教程:

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

通过本教程,我尝试解析一些论坛新闻,其中包含来自该站点(希伯来语)的大量表格:news forum

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

我最近尝试的代码是:

 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

有人可以指导我获得标题吗?

【问题讨论】:

    标签: html objective-c xcode parsing hpple


    【解决方案1】:

    不确定这是否适合您,但如果需要的表具有唯一 ID,您可以使用一种混乱的方法:将该 html 加载到 UIWebView 并通过– stringByEvaluatingJavaScriptFromString: 获取内容,如下所示:

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

    【讨论】:

      猜你喜欢
      • 2016-09-20
      • 1970-01-01
      • 1970-01-01
      • 2013-01-02
      • 2015-10-14
      • 2011-01-04
      • 1970-01-01
      相关资源
      最近更新 更多