This article uses hpple for HTML Parser.
You can also choose to use the objective-c html Parser https://github.com/zootreeves/Objective-C-HMTL-Parser
Or directly use the nsxmlparser http://www.theappcodeblog.com/2011/07/21/iphone-development-tutorial-parse-html/ of libxml2
(I have never tried either of the two)
1. Create a single view application project (with storyboard and arc)
2.Add the libxml2 library to the project.
Step 1 select the project root node in the left-side window, and a region appears next to it. Select the node in "targets"
Step 2 select "build phases" tab, expand "link binary with libraries", then click "+" button
Step 3 search "libxml2", select "libxml2.dylib", click "add" button, then libxml2.dylib will be added to the project, for the purpose of classification, we recommend that you set it to drag and drop to "frameworks" folder.
Step 4 repeat Step 1 and select "build Settings" tab, search "header search paths" and expand it. For "debug" and "release" node, click "+" button to add a value"$ {Sdk_dir}"/usr/include/libxml2(Note: This value is enclosed in double quotation marks)
Step 5: test whether libxml2 is successfully added to your project: Add the following code to your view controller. M file and check whether the compilation is successful. If the compilation succeeds, libxml2 lib can be used.
# Import <libxml/htmlparser. h>
3.Add the source code of hpple to the Project.
Step 1 download hpple from https://github.com/topfunky/hpple
Step 2: Create a group (that is, folder) named "hpple" in your project (this is for the convenience of classification and management), and then drag the following 6 files into the folder, check option "Copy items into destination group's folder", select option "create groups for any added Folders", check option "add to targets", click Finish button
- Htfpple. h
- Htfpple. m
- Htfppleelement. h
- Htfppleelement. m
- Xpathquery. h
- Xpathquery. m
Step 3: If you build the your project, there will be many "arc" compilation errors. You need to select the project root node in the left window, and a region will appear next to it. Select the node in "targets", select "build phases" tab, expand "compile sources ", three hpple values in double click. M files, and then add"-Fno-objc-Arc". Build again
Project, compiled successfully!
4.The simplest use of hpple
In your 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: Add the following code to View Controller's "viewdidload ":
[self testparser];
Step 4: Run your app!