2

Hpple を使用して HTML を解析していますが、実際には XML であることを認識していないようです (XCode デバッガーはこの変数isXML = (BOOL) NOを表示し、データを収集しません)。これを修正するにはどうすればよいですか?

これは私のコードです (他のバグの可能性もあります)。解析メソッド/関数は、最初に次のように呼び出され[ListParser parse:@"http://www.fanfiction.net/book/Harry-Potter/" at:@"//div[@=\"class\"]"];ます。

@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

4

1 に答える 1

0

を使用して XML を解析する必要がTFHppleある場合は、そうしていることを伝える必要があります。を呼び出しています+hppleWithHTMLData:。このメソッドの実装を読むと、 に設定isXMLされていることがわかりますNO。代わりに、hppleWithXMLData:メソッドを使用してください。

于 2013-03-03T05:09:20.943 に答える