5

我正在使用 Hpple 将 HTML 标记解析为 iOS。我能够解析 HTML 标签所在的数据

 <div id="NewsPageSubTitle">
         <p><**span** hi how are you>

使用ios代码:

NSString *tutorialsXpathQueryString = @"//div[@id='NewsPageArticle']/p/span ";
 NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];

但在少数情况下我没有跨度,即 html 中的字符串由标记“p”直接访问,如:

<div id="NewsPageSubTitle">
             <p>< hi how are you>

在这里,我使用 ios 代码作为:

NSString *tutorialsXpathQueryString = @"//div[@id='NewsPageArticle']/p ";
     NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];

但在这里我得到一个空白数据作为回应。

谁能让我知道如何解决这个问题?

4

3 回答 3

2

检查这个:https ://github.com/mwaterfall/MWFeedParser

这将为 iphone sdk 提供 HTML 解析器。

更多帮助:

这个博客这里

于 2012-12-28T12:43:51.403 回答
2

由于有时 para 标签有跨度,有时没有,我建议尝试通过遍历孩子来处理它

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    NSData  * data      = [NSData dataWithContentsOfFile:filePath];
    TFHpple * tutorialsParser       = [[TFHpple alloc] initWithHTMLData:data];

    NSString *tutorialsXpathQueryString = @"//div[@id='NewsPageSubTitle']";
    NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];

    for (TFHppleElement * element in tutorialsNodes) {
        NSLog(@"%@", element);
        NSLog(@"%@", [element tagName]);
        NSLog(@"%@", [element attributes]);
        NSLog(@"%@", [element children]);
        for (TFHppleElement *childElement in [element children]) {
                NSLog(@"%@", childElement);
        }
    }
于 2012-12-28T16:18:47.373 回答
0
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"html" inDirectory:@"New Folder 2"];
NSData  * data      = [NSData dataWithContentsOfFile:filePath];

NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];

NSString *htmlString = [[NSString alloc] initWithData:[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];


TFHpple * Parser       = [[TFHpple alloc] initWithHTMLData:data];

NSString *query = @"//p";

NSArray *nodes = [Parser searchWithXPathQuery:query];
for (TFHppleElement *item in nodes)
    {
       NSLog(@"Title : %@", item.content);

            NSLog(@"URL : %@", [item.attributes valueForKey:@"href"]);
      }
于 2014-05-23T08:13:14.843 回答