您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
代码:https://github.com/zootreeves/Objective-C-HMTL-Parser
?
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | NSString *str = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://vip.astro.sina.com.cn/astro/view/aries/day/20140808"] encoding:NSUTF8StringEncoding error:nil]; NSError *error; //预先步骤 //1. 在工程中添加libxml2的库(Build Phase -> Link binary with libraries -> +) //2. 在Header Search Path中添加/usr/include/libxml2 //解析HTML文档 HTMLParser *parser = [[HTMLParser alloc] initWithString:str error:&error]; if (error) { NSLog(@"%@", error); return; } //获取HTML的body部分 HTMLNode *node = [parser body]; //根据HTML节点的属性查找节点 NSArray *tabs = [node findChildrenWithAttribute:@"class" matchingName:@"tab" allowPartial:YES]; for (HTMLNode *n in tabs) { //根据节点名称查找子节点 HTMLNode *h5 = [n findChildTag:@"h5"]; HTMLNode *img = [n findChildTag:@"img"]; HTMLNode *p = [n findChildTag:@"p"]; //获取的属性值 NSLog(@"%@", [img getAttributeNamed:@"src"]); //获取节点内容 NSLog(@"%@: %@", [h5 contents], [p contents]); } |
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。