Objective-C解析HTML
来源:互联网 发布:二次元站源码 编辑:程序博客网 时间:2024/04/27 18:08
使用Objective-C解析HTML或者XML,系统自带有两种方式一个是通过libxml,一个是通过NSXMLParser。不过这两种方式都需要自己写很多编码来处理抓取下来的内容,而且不是很直观。
有一个比较好的类库hpple,它是一个轻量级的包装框架,可以很好的解决这个问题 。它是用XPath来定位和解析HTML或者XML。
安装步骤:
-加入 libxml2 到你的项目中
Menu Project->Edit Project Settings
搜索 “Header Search Paths”
添加新的 search path “${SDKROOT}/usr/include/libxml2″
Enable recursive option
-加入 libxml2 library 到你的项目
Menu Project->Edit Project Settings
搜索 “Other Linker Flags”
添加新的 search flag “-lxml2″
-将下面hpple的源代码 加入到你的项目中:
HTFpple.h
HTFpple.m
HTFppleElement.h
HTFppleElement.m
XPathQuery.h
XPathQuery.m
-XPath学习地址http://www.w3schools.com/XPath/default.asp
示例代码:
#import “TFHpple.h”
NSData *data = [[NSData alloc] initWithContentsOfFile:@”example.html”];
// Create parser
xpathParser = [[TFHpple alloc] initWithHTMLData:data];
//Get all the cells of the 2nd row of the 3rd table
NSArray *elements = [xpathParser search:@"//table[3]/tr[2]/td”];
// Access the first cell
TFHppleElement *element = [elements objectAtIndex:0];
// Get the text within the cell tag
NSString *content = [element content];
[xpathParser release];
[data release];
下载 地址:https://github.com/topfunky/hpple
另外,还有一个类似的解决方案可以参考
ElementParser http://github.com/Objective3/ElementParser
- Objective-C解析HTML
- Objective-C-解析 html 源代码
- Objective-C解析html(xml)全过程
- 在objective-c中解析html
- Objective-C XML/HTML 解析器
- 教你如何使用Objective-C解析HTML!
- 如何使用Objective-C解析HTML和XML
- 如何使用Objective-C解析HTML和XML
- 使用Objective-C HPPLE库解析HTML和XML
- 如何使用Objective-C解析HTML和XML
- 在 Objective-C 中解析 html 的代码
- 在 Objective-C 中解析 html 的代码
- 收集几个Objective-C的HTML解析库
- 收集几个Objective-C的HTML解析库
- objective-c xml解析
- 解析Objective-C反射
- Objective-C JESON解析
- objective-c内存解析
- hdu oj 2025
- iOS6 自动布局 入门–Auto Layout
- 类之间的关系
- iOS小技巧:用小图片重复显示来设置大背景图片
- iOS 6 IAP入门
- Objective-C解析HTML
- 复杂XML的解析及组装
- 使用CATiledLayer优化PDF展示
- iPhone开发中发送e-mail的3种方式
- 在 iOS 中使用 OpenSSL 库
- [ASP.NET MVC 小牛之路]04 - 依赖注入(DI)和Ninject
- warning: Signal <> missing in the sensitivity list is added for synthesis purposes
- ios获得通讯录中联系人的所有属性
- OpenSSL在苹果手机推送中的使用