tfhpple解析html中的图片,iOS第三方HTML解析 TFHpple 的简单使用

准备工作

1.导入TFHpple

2.引入静态库文件libxml2.2.dylib

3.PROJECT 中的 Search Path - header search paths添加 /usr/include/libxml2

解析步骤

1.初始化data

2.根据data创建TFHpple实例

3.查找节点存入数组

4.在该节点下 循环查找子节点

源HTML代码:

18330_normal.png?m=1345037943看了本「网球优等生」

ACG  •  zhangyi2099  •  20 分钟前  •  最后回复来自 yishanxin

1

Object-C代码

NSData *htmlData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://www.xxx.com/xxxx?x=1"]];

TFHpple *xpathParser = [[TFHpple alloc]initWithHTMLData:htmlData];

#pragma mark 每页主题

NSArray *itemArray = [xpathParser searchWithXPathQuery:@"//div[@class = 'cell item']"];

//通过for in 在itemArray数组中 循环查找子节点

for (TFHppleElement *hppleElement in itemArray) {

/***

这段被正则表达代替 @"//div[@class = 'cell item']"]

if ([[hppleElement objectForKey:@"class" ] isEqualToString:@"cell item"]) {

[self.allDataMutableArray addObject:hppleElement];

}

*/

#pragma mark 子节点头像

NSArray *IMGElementsArr = [hppleElement searchWithXPathQuery:@"//img"];

for (TFHppleElement *tempAElement in IMGElementsArr) {

NSString *imgStr = [tempAElement objectForKey:@"src"];

NSString *subStr = [@"http:" stringByAppendingString:imgStr];

[self.avatarMutableArray addObject:subStr];

}

#pragma mark 子节点标题/链接

NSArray *TitleElementArr = [hppleElement searchWithXPathQuery:@"//span[@class='item_title']"];

for (TFHppleElement *tempAElement in TitleElementArr) {

//获得标题

NSString *titleStr = [tempAElement content];

//1.获得子节点(正文连接节点) 2.获得节点属性值 3.加入到字典中

NSArray * arr = [tempAElement children];

TFHppleElement *href = arr.firstObject;

NSString * titleHrefStr = [href objectForKey:@"href"];

[self.allDataMutableDict setObject:titleStr forKey:@"title"];

self.allDataMutableDict[@"titleHref"] = titleHrefStr;

}

#pragma mark 子节点fade

//简化写法 简化3步

NSArray *nodeElementArr = [hppleElement searchWithXPathQuery:@"//a[@class='node']"];

self.allDataMutableDict[@"node"] = [nodeElementArr.firstObject content];

NSArray *fadeElementArr = [hppleElement searchWithXPathQuery:@"//span[@class = 'small fade']"];

NSArray *subArray = [ [fadeElementArr.firstObject content] componentsSeparatedByString:@"  •  "];

self.allDataMutableDict[@"louZhu"] = [subArray objectAtIndex:1];

self.allDataMutableDict[@"lastTime"] = [subArray objectAtIndex:2];

#pragma mark 子节点回复数

NSArray * repeatElementArr = [hppleElement searchWithXPathQuery:@"//a[@class = 'count_livid']"];

if ([repeatElementArr.firstObject content ]) {

self.allDataMutableDict[@"repeatCount"] = [repeatElementArr.firstObject content];

}else{

self.allDataMutableDict[@"repeatCount"] = [NSString stringWithFormat:@"%d",0];

}

#pragma mark 转化model 存进数组

[model setValuesForKeysWithDictionary:self.allDataMutableDict];

[self.allDataMutableArray addObject:model];

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值