HTML利用第三方框架HTML Parser解析

来源:互联网 发布:c语言郝斌教学视频 编辑:程序博客网 时间:2024/06/05 05:44

先导入HTML parser三方框架

添加libxml2包和路径

#import "ViewController.h"#import "HTMLParser.h"#import "HTMLNode.h"//#import "Student.h"//#import "Book.h"@interface ViewController ()            @property (weak, nonatomic) IBOutlet UIWebView *webView;@property (weak, nonatomic) IBOutlet UIImageView *imageView;@end@implementation ViewController            - (void)viewDidLoad {    [super viewDidLoad];        NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"];    //    NSString *htmlStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];    //基础框架里面的方法    //NSNumber/NSString/NSArray/NSDictionary    //NSMutableString/NSMutableArray/NSMutableDictionary    NSString *htmlStr = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];    NSLog(@"%@", htmlStr);        NSData *data = [NSData dataWithContentsOfURL:url];    NSString *str10 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];    NSLog(@"%@", str10);        NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.baidu.com/img/bd_logo.png"]];//    NSLog(@"%@", imgData);    [imgData writeToFile:@"/Users/apple/Desktop/bd_logo.png" atomically:YES];        self.imageView.image = [UIImage imageWithData:imgData];    //    NSString *imgStr = [[NSString alloc] initWithData:imgData encoding:NSUTF8StringEncoding];//    NSLog(@"<----->%@", imgStr);        HTMLParser *parser = [[HTMLParser alloc] initWithString:htmlStr error:nil];    HTMLNode *bodyNode = [parser body];//    parser = nil;        //根据class找到一个子节点    HTMLNode *abcNode = [bodyNode findChildOfClass:@"abc"];    if (abcNode) {        //获取节点里的第一个内容        NSLog(@"%@", [abcNode contents]);        //获取节点里的所有内容        NSLog(@"%@", [abcNode allContents]);    }        NSLog(@"--------");    //根据类属性查找子节点    NSArray *array = [bodyNode findChildrenOfClass:@"abc"];    for (HTMLNode *node in array) {        NSLog(@"%@", [node contents]);    }        //根据属性查找子节点    HTMLNode *aNode = [bodyNode findChildWithAttribute:@"target" matchingName:@"_blank" allowPartial:YES];    NSLog(@"tag name: %@: %@", [aNode tagName], [aNode contents]);        //根据节点名查找子节点    HTMLNode *imgNode = [bodyNode findChildTag:@"img"];    NSLog(@"%@", [imgNode rawContents]);//获取该节点的原始内容        Student *stu = [[Student alloc] init];    stu.book = [[Book alloc] init];        NSURLRequest *request = [NSURLRequest requestWithURL:url];    [self.webView loadRequest:request];}


0 0