国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

對網(wǎng)頁數(shù)據(jù)進(jìn)行提取與分析

2019-11-09 17:52:48
字體:
供稿:網(wǎng)友
首先請求數(shù)據(jù)信息
+ (void)requestWithURL:(NSString *)url                method:(NSString *)method             paramInfo:(NSDictionary *)param                handle:(void (^)(NSURLRequest *request, NSData *responseData, NSError *error))handleBlock {    // 創(chuàng)建互斥鎖    static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;    // 創(chuàng)建計數(shù)器    static NSUInteger count = 0;    // 創(chuàng)建請求對象    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];    [request setHTTPMethod:method];    [request setTimeoutInterval:20.0];    // 添加POST請求體    if ([[method uppercaseString] isEqualToString:@"POST"]) {        NSMutableString *paramStr = [NSMutableString string];        for (NSString *key in [param allKeys]) {            [paramStr appendFormat:@"%@=%@&", key, param[key]];        }        [request setHTTPBody:[[paramStr substringToIndex:paramStr.length - 1] dataUsingEncoding:NSUTF8StringEncoding]];    }    // 創(chuàng)建會話任務(wù)    NSURLsessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:            ^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {                // 檢測所有任務(wù)是否完畢                pthread_mutex_lock(&mutex);                if (--count == 0) {                                    [[UIapplication sharedApplication] setNetworkActivityIndicatorVisible:NO];                    dispatch_async(dispatch_get_main_queue(), ^{                        [[NSNotificationCenter defaultCenter] postNotificationName:@"RequestFinished" object:nil];                    });                }                pthread_mutex_unlock(&mutex);                // 回調(diào)                if (handleBlock) {                    handleBlock(request, data, error);                }            }];    [dataTask resume];    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];    // 請求計數(shù)加1    ++count;}對請求到的數(shù)據(jù)進(jìn)行解析分析,用正則進(jìn)行匹配。導(dǎo)入兩個文件分別是RegexKitLite.m,RegexKitLite.h,由于文件比較久遠(yuǎn),所以倒入后會報錯,所以要在Build phases的Compile Sources中的RegexKitLite.m加入-fno-objc-arc,另外,還要加入相應(yīng)的類庫libicucore.tbd 
+ (NSArray *)parseListWithData:(NSData *)data {    // 數(shù)據(jù)預(yù)處理    NSString *string = [self _PReprocessWithData:data];    // 創(chuàng)建返回數(shù)據(jù)    NSMutableArray *listDataArray = [NSMutableArray array];    // 匹配動態(tài)文本    NSArray  *listMatchArray = [string componentsMatchedByRegex:@"<a.*?</tr>"];    if (listMatchArray.count == 0) {        return nil;    }    // 創(chuàng)建數(shù)據(jù)模型    for (NSUInteger index = 0; index != listMatchArray.count - 1; ++index) {        // 解析數(shù)據(jù)        NSString *listRawText = listMatchArray[index];        NSString *titleText = [listRawText componentsMatchedByRegex:@"(?<=>).+?(?=</a>)"][0];        NSString *hrefText  = [listRawText componentsMatchedByRegex:@"(?<=href=/").+?(?=/")"][0];        NSString *dateText  = [listRawText componentsMatchedByRegex:@"(?<=//[)//d{4}-//d{2}-//d{2}(?=//])"][0];        // 插入數(shù)據(jù)        [listDataArray addObject:[@{@"title": [titleText stringByReplacingOccurrencesOfString:@" " withString:@""],                                    @"href":  hrefText,                                    @"date":  dateText,                                        } mutableCopy]];    }    return listDataArray;}#pragma mark - Helper+ (NSString *)_preprocessWithData:(NSData *)data {    NSString *rawStr = [[NSString alloc] initWithData:data                                             encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)];    return [rawStr stringByReplacingOccurrencesOfRegex:@"[/t/r/n]" withString:@""];}根據(jù)自己的需求相應(yīng)的得到網(wǎng)頁中的具體數(shù)據(jù)信息
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 邢台县| 旅游| 天全县| 临桂县| 新安县| 阿勒泰市| 邵阳县| 太和县| 德庆县| 理塘县| 项城市| 阿克苏市| 连江县| 周口市| 新丰县| 玉溪市| 灌阳县| 福建省| 新乐市| 宁晋县| 泗阳县| 共和县| 永顺县| 呼和浩特市| 巴彦县| 万宁市| 泽普县| 称多县| 马龙县| 房产| 吉隆县| 砚山县| 阳江市| 清徐县| 张家口市| 富裕县| 青浦区| 奉化市| 漳平市| 饶平县| 兴国县|