首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Hpple无法解析HTML

Hpple无法解析HTML
EN

Stack Overflow用户
提问于 2013-03-03 10:36:06
回答 1查看 621关注 0票数 2

我正在使用Hpple解析HTML,它似乎没有识别出它实际上是XML,而这应该是XML (XCode调试器显示这个变量isXML = (BOOL) NO,并且它不收集任何数据)。我该如何解决这个问题?

这是我的代码(它们也可能是其他的bug)。首先使用[ListParser parse:@"http://www.fanfiction.net/book/Harry-Potter/" at:@"//div[@=\"class\"]"];调用解析方法/函数

代码语言:javascript
复制
@interface ListParser () //private
+ (NSArray*) getNodeListAt: (NSURL*) page inside: (NSString*) page;
+ (NSDictionary*) getNodeData: (TFHppleElement*) node;
+ (void) addMiniListData: (NSString*) list to: (NSMutableDictionary*) dict;
@end


@implementation ListParser

+ (NSArray*) getNodeListAt: (NSURL*) page inside: (NSString*) path { // "//div[@class"z-list"]"
    NSData *data = [NSData dataWithContentsOfURL: page];
    TFHpple *listparser = [TFHpple hppleWithHTMLData:data]; //WHERE CODE SEEMS TO STOP TO WORK
    NSArray *done = [listparser searchWithXPathQuery: path];
    return done;
}

+ (void) addMiniListData: (NSString*) list to: (NSMutableDictionary*) dict{
    NSArray *parts = [list componentsSeparatedByString:@" - "];

    for(NSString* p in parts){
        NSArray* two = [p componentsSeparatedByString:@": "];
        [dict setObject:[two objectAtIndex:1] forKey:[two objectAtIndex:0]];
    }
}

+ (NSDictionary*) getNodeData: (TFHppleElement*) node{
    NSMutableDictionary* data = [NSMutableDictionary dictionary];
    [data setObject:[[[node firstChild] firstChild] objectForKey:@"href"] forKey:@"Image"];
    [data setObject:[[node firstChild] text] forKey:@"Title"];
    [data setObject:[[[[node firstChild] children] objectAtIndex:2] text] forKey:@"By"];
    [data setObject:[[[[node firstChild] childrenWithClassName:@"z-indent"] objectAtIndex:0] text] forKey:@"Summery"];
    [self addMiniListData:[[[[[[node firstChild] childrenWithClassName:@"z-indent"] objectAtIndex:0] childrenWithClassName:@"z-padtop2"] objectAtIndex:0] text] to: data];

    return data;
}

+(NSArray*) parse: (NSString*) address at: (NSString*) path{
    NSURL *url = [[NSURL alloc] initWithString:address];
    NSArray* list = [self getNodeListAt:url inside:path];
    NSMutableArray *data = [[NSMutableArray alloc] init];
    for (TFHppleElement* e in list) {
        [data addObject:[self getNodeData:e]];
    }
    return [[NSArray alloc] initWithArray: data];
}

@end

下面是我关注的教程的链接:http://www.raywenderlich.com/14172/how-to-parse-html-on-ios

EN

回答 1

Stack Overflow用户

发布于 2013-03-03 13:09:21

如果您需要使用TFHpple解析XML,您应该告诉它您正在这样做。你要给+hppleWithHTMLData:打电话。如果您阅读此方法的实现,您将看到它将isXML设置为NO。相反,请使用hppleWithXMLData:方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15181850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档