使用AFNetWorking请求数据,并用TFHpple进行解析
解析内容:示例解析的是首页中小编推荐标题栏的第一个项
- (void)wangyi { // 网易公开课地址 NSString *str=[NSString stringWithFormat:@"http://open.163.com/"]; NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; // UTF-8 NSURLRequest *request = [NSURLRequest requestWithURL:url]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request]; // 请求数据,设置成功与失败的回调函数 [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { // 请求下来的整个网页数据 NSString *html = operation.responseString; // 网页有gbk编码有utf8编码,全部换成utf8 NSString *utf8HtmlStr = [html stringByReplacingOccurrencesOfString:@"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=GBK\">" withString:@"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"]; // TFHpple解析的是data,转换成data NSData *htmlDataUTF8 = [utf8HtmlStr dataUsingEncoding:NSUTF8StringEncoding]; // 开始解析 TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlDataUTF8]; // 查找所有的 <div class="g-cell1" // 因为首页的“小编推荐”是使用该class标示的,所以主要获取的是该种形式的小课程节目 NSArray *elements = [xpathParser searchWithXPathQuery:@"//div[@class=‘g-cell1‘]"]; // 不存在则不继续执行 if ([elements count] <= 0) { return; } // 以下只是示例解析第一个课程,循环即可全部解析 TFHppleElement *first = [elements firstObject]; // 完整写法 NSArray *arr = [first searchWithXPathQuery:@"//a[1]/@href"]; TFHppleElement *ele = [arr firstObject]; NSLog(@"网址链接:%@",[ele text]); // 合并的写法 NSLog(@"图片链接:%@",[[[first searchWithXPathQuery:@"//img/@src"] firstObject] text]); NSLog(@"标题:%@",[[[first searchWithXPathQuery:@"//h5"] firstObject] text]); NSLog(@"副标题:%@",[[[first searchWithXPathQuery:@"//p"] firstObject] text]); }failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"发生错误!%@",error); }]; // 加入队列 开始执行 NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [queue addOperation:operation]; }
打印结果为
2015-07-10 20:53:10.136 wangyi[3735:210800] 网址链接:http://open.163.com/movie/2014/3/I/6/MAQ3UM8TJ_MAQ3V5JI6.html
2015-07-10 20:53:10.137 wangyi[3735:210800] 图片链接:http://imgsize.ph.126.net/?enlarge=true&imgurl=http://img1.cache.netease.com/cnews/2015/6/5/201506050950000106d.jpg_180x100x1x95.jpg
2015-07-10 20:53:10.137 wangyi[3735:210800] 标题:日本爱的产业
2015-07-10 20:53:10.137 wangyi[3735:210800] 副标题:摄制组探访日本红灯区
解析的html源码为
<div class="g-cell1"> <a href="http://open.163.com/movie/2014/3/I/6/MAQ3UM8TJ_MAQ3V5JI6.html" class="u-cover" target="_blank"> <div class="shadow rel"> <img src="http://imgsize.ph.126.net/?enlarge=true&imgurl=http://img1.cache.netease.com/cnews/2015/6/5/201506050950000106d.jpg_180x100x1x95.jpg" alt="日本爱的产业" height="100px" width="180px"> <div class="mask abs"></div> </div> </a> <a href="http://open.163.com/movie/2014/3/I/6/MAQ3UM8TJ_MAQ3V5JI6.html"> <h5 class="f-c3">日本爱的产业<span class="rec f-f1 hide"></span></h5> </a> <p class="f-c9">摄制组探访日本红灯区</p> </div>
时间: 2024-10-26 17:24:20