上一篇文章中使用到的DTHtmlParser只是简单的调用了解析接口,并没有对数据进行处理。下面这个项目Hpple则对html标签进行了类实例化。它同样是使用了libxml进行的解析操作,它还提供了搜索功能,真是开发必备的利器啊!
项目地址-->传送门
这里贴出官方使用介绍,详细功能还请程序员花时间研究哦~
#import "TFHpple.h"
NSData * data = [NSData dataWithContentsOfFile:@"index.html"];
TFHpple * doc = [[TFHpple alloc] initWithHTMLData:data];
NSArray * elements = [doc search:@"//a[@class='sponsor']"];
TFHppleElement * element = [elements objectAtIndex:0];
[e text]; // The text inside the HTML element (the content of the first text node)
[e tagName]; // "a"
[e attributes]; // NSDictionary of href, class, id, etc.
[e objectForKey:@"href"]; // Easy access to single attribute
[e firstChildWithTagName:@"b"]; // The first "b" child node