gpt4 book ai didi

ios - hpple无法解析HTML

转载 作者:行者123 更新时间:2023-12-01 16:51:40 25 4
gpt4 key购买 nike

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

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

@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

最佳答案

如果您需要使用TFHpple解析XML,则应告知您正在这样做。您正在调用+hppleWithHTMLData:。如果阅读此方法的实现,您将看到它将isXML设置为NO。而是使用hppleWithXMLData:方法。

关于ios - hpple无法解析HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15181850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com