Because of the project needs, today specifically looked at some of the HTML parsing articles, the article is not small, but some content is not very comprehensive, not enough, to add them together.
Firstly, first import the Tfhpple link in the project: Https://github.com/zgGitHub/TFHpple
1. Add LIBXML2.TBD Library
2. Add some paths in build setting in Tager find Hesder Search Paths Enter two paths one is the path of the LIBXML2.TBD library in your project; the other is $ (sdkroot)/usr/include/ Libxml2
Comment+b See if it's working properly
Two, 1, import three header files
#import " XPathQuery.h " #import " TFHpple.h " #import " TFHppleElement.h "
2. First, the HTML of the Web page is converted into NSString data that OC can recognize;
(1) If your HTML page is saved in the project, use the following method:
NSString *datastring = [NSString stringwithcontentsoffile:[[nsbundle mainbundle]pathforresource:@ " Waterfall Flow /c1>" ofType:@"html"] encoding:nsutf8stringencoding Error:nil];
(2) If the request is from the network using this method:
NSString *datastring = [NSString stringwithcontentsofurl:[nsurl urlwithstring:@ "http://www.lomowo.com/ posts/47632"] encoding:nsutf8stringencoding Error:nil];
3, if you do not want to take the entire length of the value, you can intercept the content you want
Nsrange rang1=[datastring rangeofstring:@"<div class=\"Content\">"]; Nsmutablestring*imagestr2=[[nsmutablestring alloc]initwithstring:[datastring substringfromindex:rang1.location+Rang1.length]]; Nsrange rang2=[IMAGESTR2 rangeofstring:@"<div class=\"Clear\">"]; Nsmutablestring*imagestr3=[[Nsmutablestring alloc]initwithstring:[imagestr2 substringToIndex:rang2.location]]; NSLog (@"%@", IMAGESTR3);
IMAGESTR3:
4, convert datastring into NSData, for Tfhpple class
NSData *DATA=[IMAGESTR3 datausingencoding:nsutf8stringencoding];
5, according to the label <p> find the code block you want
Tfhpple *xpathparser = [[tfhpple alloc]initwithhtmldata:data]; // Filter by Tag get all tags is <p> code block Nsarray *elements = [Xpathparser searchwithxpathquery:@ "//p"]; *contarray = [[Nsmutablearray alloc]init];
6. Get the desired label content
//start organizing Your data for(Tfhppleelement *elsementinchelements) { if([elsement content]! =Nil) { if(! [[Elsement Objectforkey:@"style"]isequaltostring:@"text-align"]) {//The filter attribute is a label with a style and a value of Text-align//print out all the contents of the node including the labelNSLog (@"%@", Elsement.raw); //print out all the contents of the node excluding tagsNSLog (@"%@", Elsement.text); } } }
The results obtained:
7. Get the desired tag attribute
//start organizing Your data for(Tfhppleelement *elsementinchelements) { if([elsement content]! =Nil) { //convert this code into a dictionary key is a property within the tag value is the property corresponding to//What you get is that tag attributes don't get the label content.Nsdictionary *elementcontent =[elsement attributes]; NSLog (@"-----------%@", elementcontent); if(Elementcontent! =Nil) {[Contarray addobject:[elementcontent Objectforkey:@"style"]]; } } }
The results obtained:
IOS Parsing HTML Data