本文是用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
step 3 這時如果你build your project,就會出現很多"ARC"編譯錯誤。你需要左邊視窗選定project root node,旁邊會出現一個地區,選擇the node in "TARGETS",select "build phases" tab, expand "Compile Sources",double click裡面的hpple的3個.m files,然後添加"-fno-objc-arc"。再次build
project,編譯成功!
4. 最簡單的使用hpple
在你的view controller .m file裡
step 1: add
#import "TFHpple.h"
step 2: add a method which use apple
- (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!