How to Parse HTML

[url]http://blog.objectgraph.com/index.php/2010/02/24/parsing-html-iphone-development/[/url]

Post Pic
[iPhone Development] How to Parse HTML
On 02.24.10, In code, iphone development, by kiichi

A few weeks ago, I was looking for a simple parser for html in iPhone because I just need to scrape a couple of webpages to get the contents. I found a nice wrapper on this posting, and it’s called hpple. Simple steps to use the library.

Include and Link libxml2

[list]
[*] 1. Expand Targets
[*] 2. Double Click on your project name
[*] 3. Select All Configuration
[*] 4. Search for Header Search Path
[*] 5. Add this line below with recursive option
[*]
[*] ${SDKROOT}/usr/include/libxml2
[*]
[*] 6. Search for Other Linker Flag
[*] 7. Add this line below
[*]
[*] -lxml2
[/list]

See screenshots below

html_include

[img]http://blog.objectgraph.com/wp-content/uploads/2010/02/html_include.png[/img]

html_linking

[img]http://blog.objectgraph.com/wp-content/uploads/2010/02/html_linking1.png[/img]


Download Source Codes

git clone git://github.com/topfunky/hpple.git

Then drag and drop following source codes

[list]
[*]TFHpple.h
[*]TFHpple.m
[*]TFHppleElement.h
[*]TFHppleElement.m
[*]XPathQuery.h
[*]XPathQuery.m
[/list]

That’s it. Let’s write some codes.

[NSString stringWithContentsOfURL:[NSURL URLWithString: @"http://www.objectgraph.com/contact.html"] dataUsingEncoding:NSUTF8StringEncoding];
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
NSArray *elements = [xpathParser search:@"//h3"]; // get the page title - this is xpath notation
TFHppleElement *element = [elements objectAtIndex:0];
NSString *myTitle = [element content];
NSLog(myTitle);
[xpathParser release];
[htmlData release];


XCode Project Download

Donwnload the complete project file is available here.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值