HTML解析类TFHpple框架

TFhpple框架github

  • 结构如下
          
         
                   
          
         
                   
          
         
  • 以获取文章列表标题 简介 内容为例
    自定义model如下
@property (nonatomic, copy)NSString *title;
@property (nonatomic, copy)NSString *desc;
@property (nonatomic, copy)NSString *content;
  • 开始解析
- (NSMutableArray *)praseWithTFHpple:(TFHpple *)hppleObject {
    NSMutableArray *tempArray = [@[] mutableCopy];
    NSArray *divArr = [hppleObject searchWithXPathQuery:@"//div"];
    for (TFHppleElement *element in divArr) {
        if ([[element objectForKey:@"class"] isEqualToString:@"moving_article clearfix"]) {
            ArticleListModel *articleModel = [[ArticleListModel alloc] init];
            //获取title
            TFHppleElement *h2Element = [[element searchWithXPathQuery:@"//h2"] firstObject];
            articleModel.title = [h2Element content];   
            //获取desc
            NSArray *pArray = [element searchWithXPathQuery:@"//p"];
            for (TFHppleElement *pElement in pArray) {
                if ([[pElement objectForKey:@"class"] isEqualToString:@"clearfix"]) {
                    articleModel.desc = [[pElement content] stringByReplacingOccurrencesOfString:@"\n" withString:@""];
                }
            } 
            //获取content
            NSArray *_divArray = [element searchWithXPathQuery:@"//div"];
            for (TFHppleElement *_divElement in _divArray) {
                if ([[_divElement objectForKey:@"class"] isEqualToString:@"long"]) {
                    articleModel.content = [_divElement content];
                }
            }  
           NSLog(@"%@",[articleModel descroption]);
        }
    }
    return tempArray;
}

你可能感兴趣的:(HTML解析类TFHpple框架)