#define PARSER_URL @"http://www.baidu.com/s?wd=iphone&rsv_bp=0&ch=&tn=baidu&bar=&rsv_spt=3&ie=utf-8&rsv_sug3=5&rsv_sug=0&rsv_sug1=5&rsv_sug4=115&inputT=1438"
-(void) parser{
NSString *urlString = [NSString stringWithContentsOfURL:[NSURL URLWithString:PARSER_URL] encoding:NSUTF8StringEncoding error:nil];
NSData *data = [urlString dataUsingEncoding:NSUTF8StringEncoding];
// Create parser
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:data];
//Get all the cells of the 2nd row of the 3rd table
NSString *xpathQuery = @"//*[@id='container']/p/span";
NSArray *elements = [xpathParser searchWithXPathQuery:xpathQuery];
NSLog(@"elements.count -->%i",[elements count]);
// Access the first cell
// Get the text within the cell tag
for (TFHppleElement * element in elements) {
NSLog(@"element------------->%@", element);
NSLog(@"element.tagName----->%@", [element tagName]);
NSLog(@"element.attribute--->%@", [element attributes]);
NSLog(@"element.child------->%@", [element children]);
for (TFHppleElement *childElement in [element children]) {
NSLog(@"childElement.text-->%@", childElement.content);
[self.label setText:childElement.content];
}
}
}
一.使用 TFHpple 解析库
来源地址:TFHpple lIB
感谢stack overflow Vineet Bhatia 网友的热心回答.
来源: ios html parser
二 . 使用HTMLParser 类库解析
add "/usr/include/libxml2"
来源: HTMLParser