iOS: 用libxml2 and hpple来做html parser
来源:互联网 发布:阿里云主机禁止ip访问 编辑:程序博客网 时间:2024/06/06 05:10
本文是用hpple来做html parser
你还可以选择用objective-c html parser https://github.com/zootreeves/Objective-C-HMTL-Parser
或直接用libxml2的NSXMLParser http://www.theappcodeblog.com/2011/07/21/iphone-development-tutorial-parse-html/
(这2个我都没试过)
1. 创建一个single view application project (with storyboard and arc)
2. 把libxml2 library加到project里。
step 1 左边窗口选定project root node,旁边会出现一个区域,选择the node in "TARGETS"
step 2 select "build phases" tab, expand "Link Binary With Libraries",然后click "+" button
step 3 search "libxml2", 选定"libxml2.dylib", click "add" button,这时libxml2.dylib会添加到project,出于归类的目的,建议把它drag and drop to "Frameworks" folder。
step 4 重复step 1, 然后选定"Build Settings" tab,search "Header Search Paths"并expand it,对于"debug" and "release" node,均通过click "+" button来添加一个value为"${SDK_DIR}"/usr/include/libxml2的item (注意:该值是带有双引号的)
step 5 简单测试你的project是否添加libxml2成功:在你的view controller .m file里添加下列代码,然后看看是否编译成功,若成功则表示可以使用libxml2 lib
#import <libxml/HTMLparser.h>
3. 把hpple的源码添加到project。
step 1 下载hpple from https://github.com/topfunky/hpple
step 2 在你的project里create a group (即folder) named "hpple" (这是出于归类便于管理的目的),然后把下列6个files拖拽进该folder,然后勾上option "copy items into destination group's folder", 选择option "Create groups for any added folders", 勾上option "Add to Targets", click Finish button
- HTFpple.h
- HTFpple.m
- HTFppleElement.h
- HTFppleElement.m
- XPathQuery.h
- XPathQuery.m
4. 最简单的使用hpple
在你的view controller .m file里
step 1: add
- #import "TFHpple.h"
- - (void) testparser{
- // NSString *htmlString=[NSString stringWithContentsOfURL:[NSURL URLWithString: @"https://cap.cityu.edu.hk/default.aspx"] encoding: NSUTF8StringEncoding error:nil];
- NSString *htmlString=[NSString stringWithContentsOfURL:[NSURL URLWithString: @"http://www.cwb.gov.tw/eng/index.htm"] encoding: NSUTF8StringEncoding error:nil];
- NSData *htmlData=[htmlString dataUsingEncoding:NSUTF8StringEncoding];
- TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
- NSArray *elements = [xpathParser searchWithXPathQuery:@"//title"]; // get the title
- //NSArray *elements = [xpathParser searchWithXPathQuery:@"//td[@class='compact']/a"];
- TFHppleElement *element = [elements objectAtIndex:0];
- NSString *elementContent = [element content];
- NSLog(@"result = %@",elementContent);
- }
step 3: 在view controller的“viewDidLoad”里添加下列代码
- [self testparser];
step 4: run your app!
- iOS: 用libxml2 and hpple来做html parser
- iOS: 用libxml2 and hpple来做html parser
- iOS: 用libxml2 and hpple来做html parser
- ios hpple解析html demo
- libxml2, the XML C parser and toolkit
- HTML and URL Parser
- HTML and URL Parser
- 用html parser 来获取HTML网页中Form的各个属性名 值组
- HTML parser
- html parser
- HTML::Parser
- iPhone:使用hpple解析html,Xpath导航
- iPhone:使用hpple解析html,Xpath导航
- 使用libxml解析HTML -- Hpple
- ios hpple通过xpath抓取数据。
- 使用Objective-C HPPLE库解析HTML和XML
- OC解析html的类库Hpple的一些方法
- HTML Parser 源代码
- Javascript 中 null、NaN和undefined的区别
- VOIP 带宽计算
- linux嵌入式系统下实现U盘、SD卡自动挂载功能
- CentOS设置中文输入法
- ios之关键字retain / copy / mutableCopy 使用技巧
- iOS: 用libxml2 and hpple来做html parser
- JS模块化开发:使用SeaJs高效构建页面
- frame、bounds和center的使用场景
- init_MUTEX 替换为 sema_init(sem,1)
- 手动内存管理转ARC项目实战
- 详细讲解Hadoop中的一个简单数据库HBase
- iOS_解决demo真机运行不了问题
- 轻易搞定外盘黄金交易,你将进入每月收益远高于你的工作收入!
- UBIFS介绍