代码:https://github.com/zootreeves/Objective-C-HMTL-Parser
?
戴维营教育代码01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | NSString *str = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://vip.astro.sina.com.cn/astro/view/aries/day/20140808"] encoding:NSUTF8StringEncoding error:nil];
NSError *error;
HTMLParser *parser = [[HTMLParser alloc] initWithString:str error:&error];
if (error) {
NSLog(@"%@", error);
return;
}
HTMLNode *node = [parser body];
NSArray *tabs = [node findChildrenWithAttribute:@"class" matchingName:@"tab" allowPartial:YES];
for (HTMLNode *n in tabs) {
HTMLNode *h5 = [n findChildTag:@"h5"];
HTMLNode *img = [n findChildTag:@"img"];
HTMLNode *p = [n findChildTag:@"p"];
NSLog(@"%@", [img getAttributeNamed:@"src"]);
NSLog(@"%@: %@", [h5 contents], [p contents]);
}
|