TFHpple是一個XML/HTML解析框架
解析HTML中的內(nèi)容分四步:
- 將HTML數(shù)據(jù)轉(zhuǎn)換為NSData類型例证;
- 根據(jù)data創(chuàng)建TFHpple實例疹味;
- 查找節(jié)點存入數(shù)組雕擂;
- 從數(shù)組中取出節(jié)點量没。
代碼
NSString *url = @"http://www.reibang.com/u/e163bc6048d8";
//將網(wǎng)址轉(zhuǎn)化為data數(shù)據(jù)
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
//創(chuàng)建解析對象
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:data];
NSArray *dataArr = [xpathParser searchWithXPathQuery:@"http://a"];
for (TFHppleElement *element in dataArr) {
if ([[element objectForKey:@"class"] isEqualToString:@"title"]) {
NSLog(@"%@\n",element.text);
}
}
控制臺打印
demo鏈接:https://github.com/tqhnet/IOS-TFHppleTest