iOS开发之第三方库的学习--hpple的使用

前言:因为在开发中很可能会遇到html解析,如果后台提供的数据只有html数据,或者开发的app需要从web前端的html里获取数据,就需要html解析工具了。

关于HTML解析库,可以阅读:收集几个Objective-C的HTML解析库 了解。下面是我在学习开源项目Coding遇到用到了hpple解析工具,所以就拿出来学习然后总结总结一下了。

新建一个普通的工程,然后我使用了pod集成这个hpple库来使用:

iOS开发之第三方库的学习--hpple的使用

然后我累解析解析index.html文件:

iOS开发之第三方库的学习--hpple的使用

主要的代码:

 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSData * data = [NSData dataWithContentsOfFile:@"/Users/HeYang/Documents/Xcode/object-c/第三方库hpple的学习/hppleTest/hppleTest/index.html"];
NSLog(@"数据:%@",data);
TFHpple * doc = [[TFHpple alloc] initWithHTMLData:data]; NSArray * elements = [doc searchWithXPathQuery:@"//a"]; NSLog(@"a节点个数:%ld",elements.count);
for (int i = ; i < elements.count; i++) {
TFHppleElement * e = [elements objectAtIndex:i];
NSLog(@"1:%@",[e text]); // The text inside the HTML element (the content of the first text node)
NSLog(@"2:%@",[e tagName]); // "a"
NSLog(@"3:%@",[e attributes]); // NSDictionary of href, class, id, etc.
NSLog(@"4:%@",[e objectForKey:@"href"]); // Easy access to single attribute
NSLog(@"5:%@",[e firstChildWithTagName:@"img"]); // The first "b" child node
NSLog(@"===========解析完毕一次===========");
}
}

打印结果是:

-- ::00.251 hppleTest[:] 数据:<3c703ee4 b88be58d 88e4ba94 e782b9e5 a49ae5b0 b1e588b0 e8bebee4 ba86e79b aee79a84 e59cb0e7 a8bbe59f 8ee58ebf e59f8ee5 a283e586 85efbc8c e4bd8fe5 9ca8e4ba 86e4b880 e4b8aae5 85b7e69c 89e5be88 e5a5bde5 9cb0e790 86e4bd8d e7bdaee7 9a84e985 92e5ba97 efbc8ce4 bd86e698 afe6b2a1 e69c89e7 bd91e7bb 9ce6b2a1 e69c89e7 83ade6b0 b4efbc8c e68891e5 a5bde583 8fe9ab98 e58f8de4 ba86efbc 8ce4b880 e5a4a9e5 9d90e8bd a6efbc8c e79c8be5 88b0e4ba 86e8aeb8 e5a49ae7 9a84e7be 8ee4b8bd e699afe8 89b2efbc 8ce5a5bd e6bc82e4 baaee5a5 bde6bc82 e4baaeef bc8ce698 8ee5a4a9 e699afe5 8cbae79a 84e69c80 e4bd8ee6 b5b7e68b 94e59b9b e58d83e4 b8a4e799 bee5a49a e7b1b3ef bc8ce5b8 8ce69c9b e4b88de8 a681e9ab 98e58f8d e4ba86e2 80a6e280 a60a2020 20203c61  663d2268  3a2f2f64 6e2d636f 64696e67 2d6e6574 2d70726f  696f6e2d 70702e71 626f782e 6d652f62  3537392d  2d343633 642d6235 34662d35   6665622e 6a706722  6765743d 225f626c 616e6b22 20636c61 73733d22  6c652d6d 61726b64 6f776e2d 696d6167 652d6c69 6e6b2220 72656c3d 226e6f66 6f6c6c6f 77223e0a   3c696d67  3d226874 7470733a 2f2f646e 2d636f64 696e672d 6e65742d 70726f64  6f6e2d70 702e7162 6f782e6d 652f6265  37392d64 6339642d  2d623534 662d3531   65622e6a  616c743d  6c617373 3d226275 62626c65 2d6d6172 6b646f77 6e2d696d  3e0a2020 20203c2f 613e0a20 2020203c  65663d22  733a2f2f 646e2d63 6f64696e 672d6e65 742d7072 6f647563 74696f6e 2d70702e 71626f78 2e6d652f   2d636263 642d3466 63612d38 3632372d    2e6a7067   3d225f62 6c616e6b 2220636c 6173733d  626c652d 6d61726b 646f776e 2d696d61 67652d6c 696e6b22 2072656c 3d226e6f 666f6c6c 6f77223e 0a202020 203c696d  633d2268  3a2f2f64 6e2d636f 64696e67 2d6e6574 2d70726f  696f6e2d 70702e71 626f782e 6d652f30  6330352d  2d346663 612d3836 32372d39   3232662e 6a706722 20616c74 3d222220 636c6173 733d2262 7562626c 652d6d61 726b646f 776e2d69 6d616765 223e0a20 2020203c 2f613e0a 3c2f703e>
-- ::00.255 hppleTest[:] a节点个数:
-- ::00.256 hppleTest[:] : -- ::00.256 hppleTest[:] :a2016-- ::00.258 hppleTest[:] :{
nodeAttributeArray = (
{
attributeName = src;
nodeContent = "https://dn-coding-net-production-pp.qbox.me/bef3c579-dc9d-463d-b54f-51da3391cfeb.jpg";
},
{
attributeName = alt;
nodeContent = "";
},
{
attributeName = class;
nodeContent = "bubble-markdown-image";
}
);
nodeName = img;
raw = "<img src=\"https://dn-coding-net-production-pp.qbox.me/bef3c579-dc9d-463d-b54f-51da3391cfeb.jpg\" alt=\"\" class=\"bubble-markdown-image\"/>";
}
-- ::00.258 hppleTest[:] ===========解析完毕一次===========
-- ::00.258 hppleTest[:] : -- ::00.259 hppleTest[:] :a
-- ::00.259 hppleTest[:] :{
class = "bubble-markdown-image-link";
href = "https://dn-coding-net-production-pp.qbox.me/05309c05-cbcd-4fca-8627-9143b27b922f.jpg";
rel = nofollow;
target = "_blank";
}
-- ::00.259 hppleTest[:] :https://dn-coding-net-production-pp.qbox.me/05309c05-cbcd-4fca-8627-9143b27b922f.jpg
-- ::00.260 hppleTest[:] :{
nodeAttributeArray = (
{
attributeName = src;
nodeContent = "https://dn-coding-net-production-pp.qbox.me/05309c05-cbcd-4fca-8627-9143b27b922f.jpg";
},
{
attributeName = alt;
nodeContent = "";
},
{
attributeName = class;
nodeContent = "bubble-markdown-image";
}
);
nodeName = img;
raw = "<img src=\"https://dn-coding-net-production-pp.qbox.me/05309c05-cbcd-4fca-8627-9143b27b922f.jpg\" alt=\"\" class=\"bubble-markdown-image\"/>";
}

项目源码链接: http://pan.baidu.com/s/1eSK58eY 密码: g4ic

上一篇:listview的tag


下一篇:KMP算法-Python版