使用TFHelp解析Html

似乎是第一次使用TFHelp解析Html,也是第一次解析Html遇到挺多的难题,现在这里简单的总结一下,慢慢补充TFHelp的使用!

https://github.com/topfunky/hpple 下载TFhelp框架

然后将下面hpple的源代码加入到你的项目中:
HTFpple.h
HTFpple.m
HTFppleElement.h
HTFppleElement.m
XPathQuery.h
XPathQuery.m

一) libxml2 到你的项目中

到Project设置中,选all,找到Search paths下面的
Header Search Paths项目
添加新的 search path “${SDKROOT}/usr/include/libxml2″
注意选择 Enable recursive option

二)加入 libxml2 library 到你的项目
到Target中,选择Build Phases页
在Link Binary With Libraries中
从列表中选择libxml2.tbd

然后到使用文件中导包:

#import "TFHpple.h"

这样就可以使用了!

先来一个简单的表签解析

 

 NSString *str = @"http://www.cnblogs.com/wq-gril/";
    
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:str]];

 TFHpple * doc       = [[TFHpple alloc] initWithHTMLData:data];

 NSArray *elements = [doc searchWithXPathQuery:@"//titile"];
  for (TFHppleElement * hppleElement in elements) {

     NSLog(@"%@",hppleElement.raw);         
    NSLog(@"%@",hppleElement.text);

 }
    

输出日志:

<title>牵着蜗牛走的我 - 博客园</title>

牵着蜗牛走的我 - 博客园

 

其实html主要解析的就是图片 但是这个是个难点,当然对于我来说 ,所以稍等!

 

posted @ 2016-02-19 11:31  牵着蜗牛走的我  阅读(431)  评论(0)    收藏  举报