2
我正在使用Hpple解析HTML,它似乎無法識別它實際上是XML,它應該(XCode調試器顯示此變量isXML = (BOOL) NO
並且它不收集任何數據) 。我該如何解決?Hpple無法解析HTML
這是我的代碼(它們也可能是其他的錯誤)。解析方法/函數與[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
HTML不是XML。 – nneonneo 2013-03-03 02:36:53
@nneonneo我知道,但據稱這是假設爲兩者工作。至少根據其網站和幾個教程,它說它會將HTML識別爲XML。 – 2013-03-03 02:38:21