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;
    }
    



  • 相关阅读:
    为什么使用内部类?怎样使用内部类? 2016年12月15号
    java内部类 2016年12月13号
    接口与抽象类的区别与联系 2016年12月13日
    多态的向上转型和向下转型 2016.12.8
    构造器的调用顺序 2016.12.8
    static final 和final的区别 2016.12.07
    根据进程号查询占用资源多的线程
    Intellij idea启动项目提示"ClassNotFoundException"
    IntelliJ IDEA setup JDK无效
    (转)面试合集
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5142180.html
Copyright © 2011-2022 走看看