ios hpple解析html demo

来源:互联网 发布:说明书排版设计软件 编辑:程序博客网 时间:2024/05/17 02:46


//  rootViewController.m

//  QduniversityJobhelper

//

//  Created by niuxinghua on 14-12-17.

//  Copyright (c) 2014 niuxinghua. All rights reserved.

//


#import "rootViewController.h"

#import "Config.h"

#import "AFNetworking.h"

#import "TFHpple.h"

#import "TFHppleElement.h"

#import "XPathQuery.h"

#import "TFHpple.h"

#import "JobBean.h"

#import "myCell.h"

@interface rootViewController ()


@end


@implementation rootViewController


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

    CGFloat width=mainWidth;

    CGFloat height=mainHeight;

    _datalist=[[NSMutableArrayalloc]init];

    // Do any additional setup after loading the view.

    _button=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    _button.frame=CGRectMake(width/2-40,80,80, 40);

    [_buttonsetTitle:@"load"forState:UIControlStateNormal];

    [_buttonaddTarget:selfaction:@selector(load)forControlEvents:UIControlEventTouchUpInside];

    _text=[[UITextFieldalloc]initWithFrame:CGRectMake(0,120, width, height-120)];

    _table=[[UITableViewalloc]initWithFrame:CGRectMake(0,120, width, height-120)style:UITableViewStylePlain];

    _table.delegate=self;

    _table.dataSource=self;

    [self.view addSubview:_button];

    [self.view addSubview:_table];

}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;

}

- (NSInteger)tableView:(UITableView *)tableView

 numberOfRowsInSection:(NSInteger)section{

    return [_datalistcount];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *reuseIdetify =@"SvTableViewCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdetify];

    if (!cell) {

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reuseIdetify];

        cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;

        cell.showsReorderControl =YES;

     

    }

    NSInteger row = [indexPath row];


    JobBean* bean=[_datalistobjectAtIndex:row];

    

    cell.textLabel.backgroundColor = [UIColorclearColor];

    cell.textLabel.text = bean.jobTitle;

    

   // JobBean* bean=[_datalist objectAtIndex:row];

    

    

    

    

    return cell;

}

-(void)load{

    

    NSString *urlstring=@"http://career.ouc.edu.cn/recruitment/campus/";//此处网址不对,只是示意可以生成一个动态的urlstring

    NSURL *url = [NSURLURLWithString:urlstring];

    NSData *data = [NSDatadataWithContentsOfURL:url];

    TFHpple * doc       = [[TFHpplealloc]initWithHTMLData:data];

    NSArray * elements  = [doc searchWithXPathQuery:@"//a"];

    for (int i=10; i<[elementscount]-27; i++) {

        TFHppleElement * element = [elementsobjectAtIndex:i];

        NSString *links=[element raw];

        NSArray* href=[linkscomponentsSeparatedByString:@" "];

        NSString *link=[href objectAtIndex:1];

        NSString *link1=[link substringFromIndex:6];

        int length=[link1 length]-1;

        NSString *reallink=[link1 substringToIndex:length];

        NSLog(@"%@",[elementtext]);

        NSLog(@"%@",reallink);

        JobBean *bean=[[JobBeanalloc]init];

        bean.jobTitle=[element text];

        bean.jobDetilurl=reallink;

        [_datalist addObject:bean];


    }

    

    

    [_tablereloadData];

}

-(NSString *)urlString:(NSString *)value{

    NSURL *url = [NSURLURLWithString:value];

    NSData *data = [NSDatadataWithContentsOfURL:url];

    //解决中文乱码,GBK

    NSStringEncoding enc =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);

    NSString *retStr = [[NSStringalloc]initWithData:data encoding:enc];

    return retStr;  

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end

0 0
原创粉丝点击