首先把贤哥封装好的解析文件和https://github.com/topfunky/hpple上下载的文件夹拖进工程里
添加 libxml2.dylib 框架 在 Header Search Paths 中添加 /usr/include/libxml2 引入头文件YXHTML.h
// 获取数据 NSData *data = [YXHTML htmlData:@"http://www.cnblogs.com/YouXianMing/"];
// 创建解析器 YXHTML *html = [[YXHTML alloc] initWithHTMLData:data];
// 判断能否解析 if (html.isAccess == YES) { // 获取数据中的所有a节点 [html serachWithNode:@"//a" each:^(BOOL sucess, TFHppleElement *element) { if (sucess) { // 获取所有a节点中的节点属性href的值为http://www.cnblogs.com/YouXianMing/中的节点 if ([element.attributes[@"href"] \ isEqualToString:@"http://www.cnblogs.com/YouXianMing/"]) { // 获取该节点内容 NSLog(@"%@", element.text); } } }]; } else { NSLog(@"无法解析"); }
如果自己忘了就查看备用->demo->解析HTML
详情请鉴http://www.cnblogs.com/YouXianMing/p/3731866.html 感谢贤哥的分享~
|