美文网首页iOS
HTML解析类TFHpple框架

HTML解析类TFHpple框架

作者: cocoawork丶 | 来源:发表于2016-11-24 13:43 被阅读110次

    TFhpple框架github

    • 结构如下
              <div class="album_box clearfix">
                <div class="a_left">
                  <a href="/album/550fcb41dfa688445500002c">
                    ![](https://img.haomeiwen.com/i1933166/da7fb99c21b410d3.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
                    <p>连载中</p>
                  </a>
                </div>
                 
                <div class="a_right">
                 <h1><a href="/album/550fcb41dfa688445500002c">青春的疲倦</a></h1>
                 <p>作者:<a href="/profile/1179643" class="blue">骆瑞生Rhettson</a>
                        
                 </p>
                 <p>分集:6<span class="genre">类型:故事</span></p>
                 <div class="a_article">
                  <p><a href="/album/550fcb41dfa688445500002c">骆瑞生的小说、文章与诗词。</a></p>
                              </div>
                  <div class="a_idea">
                   <a>阅读:<span class="num">7739</span></a>
                   <a>推荐:<span class="num">28</span></a>
                  </div>
                 </div>
                 
              </div>
             <!--album_box end-->
                       <!--album_box start-->
              <div class="album_box clearfix">
                 
                <div class="a_left">
                  <a href="/album/551bb249dfa688b519000002">
                    ![](https://img.haomeiwen.com/i1933166/80773ffc1dae17a1.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
                    <p>连载中</p>
                  </a>
                </div>
                 
                <div class="a_right">
                 <h1><a href="/album/551bb249dfa688b519000002">实时评论——书籍电影</a></h1>
                 <p>作者:<a href="/profile/567917" class="blue">YishuiH-AN易水涵</a>
                        
                 </p>
                 <p>分集:38<span class="genre">类型:随笔</span></p>
                 <div class="a_article">
                  <p><a href="/album/551bb249dfa688b519000002">这里是一些电影与书籍的整合推荐及书影评论 当然也会择选一些以往好的文艺产物分享 如果你需要文艺的情怀来冲淡当下的物欲生活 不妨进来看看这些作家、歌手、导演、专辑、电影、小说、散文……</a></p>
                              </div>
                  <div class="a_idea">
                   <a>阅读:<span class="num">185033</span></a>
                   <a>推荐:<span class="num">5231</span></a>
                  </div>
                 </div>
                 
              </div>
             <!--album_box end-->
                       <!--album_box start-->
              <div class="album_box clearfix">
                 
                <div class="a_left">
                  <a href="/album/5515ebeddfa688bb24000041">
                    ![](https://img.haomeiwen.com/i1933166/a1b5845ff3fb1f54.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
                    <p>连载中</p>
                  </a>
                </div>
                 
                <div class="a_right">
                 <h1><a href="/album/5515ebeddfa688bb24000041">梦想开始的地方</a></h1>
                 <p>作者:<a href="/profile/1167833" class="blue">郭道甲</a>
                        
                 </p>
                 <p>分集:52<span class="genre">类型:故事</span></p>
                 <div class="a_article">
                  <p><a href="/album/5515ebeddfa688bb24000041">每一篇故事,都有你曾经的影子。</a></p>
                              </div>
                  <div class="a_idea">
                   <a>阅读:<span class="num">166656</span></a>
                   <a>推荐:<span class="num">6158</span></a>
                  </div>
                 </div>
                 
              </div>
             
    
    • 以获取文章列表标题 简介 内容为例
      自定义model如下
    @property (nonatomic, copy)NSString *title;
    @property (nonatomic, copy)NSString *desc;
    @property (nonatomic, copy)NSString *content;
    
    • 开始解析
    - (NSMutableArray *)praseWithTFHpple:(TFHpple *)hppleObject {
        NSMutableArray<ArticleListModel *> *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框架

        本文链接:https://www.haomeiwen.com/subject/tapbpttx.html