• 万方数据知识平台 TFHpple +Xpath解析


    试了一下。基本上适合全部的检索结果。

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        //获取数据
        NSURL *url=[NSURL URLWithString:@"http://s.g.wanfangdata.com.cn/Paper.aspx?q=hiv"];
        NSURLRequest *request=[NSURLRequest requestWithURL:url];
        NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        
        //解析数据
        TFHpple *hpple=[TFHpple hppleWithHTMLData:data];
        
        //XPATH字符串 越短越好。可是要能过滤出符合要求的结果
        //xpath教程:http://www.w3cschool.cn/index-14.html
        NSString *xpathStr =@"//div/ul/li [@class='title_li']   /a [not(@class)]";//解析标题 
        NSString *xpathStr2 =@"//div/ul/li [@class='zi'] ";//解析简单介绍
        NSArray *arr=[hpple searchWithXPathQuery:xpathStr];
        NSArray *arr2=[hpple searchWithXPathQuery:xpathStr2];
        //NSLog(@"%@",arr2);
        //这个数组中就有须要的值
       
        alltilte=[[NSMutableArray alloc]init];
        alltext=[[NSMutableArray alloc]init];
        alltilte = [self SaveArry:arr];
        alltext = [self SaveAlltext:arr2];
       
        for(int i=0;i<alltilte.count;i++)
        {
            NSLog(@"标题%@",alltilte[i]);
            NSLog(@"简单介绍%@",alltext[i]);
        }
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
        
    }
    
    -(NSMutableArray *)SaveAlltext :(NSArray *)_arry
    {
        for (TFHppleElement *tmp in _arry)
        {
            NSString * string =[tmp raw];
            
            NSRange range;
            //        NSRange range2;
            range = [string rangeOfString:@"<br />"];
            string = [string substringToIndex:range.location];
            
            NSString  *temp =[string stringByReplacingOccurrencesOfString:@"<li class="zi">
    "
                                                               withString:@""];
            temp =[temp stringByReplacingOccurrencesOfString:@"<font color="red">"
                                                  withString:@""];
            temp =[temp stringByReplacingOccurrencesOfString:@"</font>"
                                                  withString:@""];
            temp =[temp stringByReplacingOccurrencesOfString:@"<li class="zi">"
                                                  withString:@""];
            [alltext addObject:temp];
        }
        return alltext;
    }
    
    
    
    -(NSMutableArray *)SaveArry: (NSArray *)_arry
    {
        
        for (TFHppleElement *tmp in _arry)
        {
            NSString * string =[tmp raw];
            NSRange range;
            NSRange range2;
            range = [string rangeOfString:@"</a>"];
            string = [string substringToIndex:range.location];
            NSString  *temp =[string stringByReplacingOccurrencesOfString:@"<font color="red">" withString:@""];
            temp=[temp stringByReplacingOccurrencesOfString:@"</font>" withString:@""];
            range = [string rangeOfString:@">"];
            range2 = [string rangeOfString:@"img"];
            if(range.length!=0&&range2.length==0)
            {
                string = [temp substringFromIndex:range.location+1];
               [alltilte addObject:string];
            }
            
        }
    
        
        return alltilte;
    }
    



  • 相关阅读:
    2021.07.14牛客学习
    2021.07.13学习总结
    new和malloc区别(自己口头描述)以及delete用法
    排序整理(c++实现),搭配图解
    如何将bilibili上缓存的文件转成MP4
    第07组 团队Git现场编程实战
    第二次结队编程作业
    团队项目-需求分析报告
    团队项目-选题报告
    第一次结对编程作业
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5142180.html
Copyright © 2020-2023  润新知