zoukankan      html  css  js  c++  java
  • 万方数据知识平台 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;
    }
    



  • 相关阅读:
    ARMV8 datasheet学习笔记3:AArch64应用级体系结构之Memory order
    ARMV8 datasheet学习笔记3:AArch64应用级体系结构之Atomicity
    ARMV8 datasheet学习笔记3:AArch64应用级体系结构
    ARMV8 datasheet学习笔记2:概述
    最短路径
    网络流
    二分图
    zabbix 3.4新功能值预处理
    zabbix 3.4新功能值解析——Preprocessing预处理
    Zabbix监控windows的CPU利用率和其他资源
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5142180.html
Copyright © 2011-2022 走看看