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

首頁 > 學院 > 開發設計 > 正文

對網頁數據進行提取與分析

2019-11-09 16:53:16
字體:
來源:轉載
供稿:網友
首先請求數據信息
+ (void)requestWithURL:(NSString *)url                method:(NSString *)method             paramInfo:(NSDictionary *)param                handle:(void (^)(NSURLRequest *request, NSData *responseData, NSError *error))handleBlock {    // 創建互斥鎖    static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;    // 創建計數器    static NSUInteger count = 0;    // 創建請求對象    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]];    }    // 創建會話任務    NSURLsessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:            ^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {                // 檢測所有任務是否完畢                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);                // 回調                if (handleBlock) {                    handleBlock(request, data, error);                }            }];    [dataTask resume];    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];    // 請求計數加1    ++count;}對請求到的數據進行解析分析,用正則進行匹配。導入兩個文件分別是RegexKitLite.m,RegexKitLite.h,由于文件比較久遠,所以倒入后會報錯,所以要在Build phases的Compile Sources中的RegexKitLite.m加入-fno-objc-arc,另外,還要加入相應的類庫libicucore.tbd 
+ (NSArray *)parseListWithData:(NSData *)data {    // 數據預處理    NSString *string = [self _PReprocessWithData:data];    // 創建返回數據    NSMutableArray *listDataArray = [NSMutableArray array];    // 匹配動態文本    NSArray  *listMatchArray = [string componentsMatchedByRegex:@"<a.*?</tr>"];    if (listMatchArray.count == 0) {        return nil;    }    // 創建數據模型    for (NSUInteger index = 0; index != listMatchArray.count - 1; ++index) {        // 解析數據        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];        // 插入數據        [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:@""];}根據自己的需求相應的得到網頁中的具體數據信息
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 济南市| 城市| 新营市| 乌拉特前旗| 始兴县| 平陆县| 宁蒗| 四会市| 澎湖县| 嘉定区| 丘北县| 民勤县| 邢台市| 时尚| 津南区| 荔波县| 峨山| 遂平县| 南雄市| 长丰县| 开鲁县| 达日县| 平阳县| 邮箱| 富川| 商丘市| 丹凤县| 乃东县| 芒康县| 台中市| 收藏| 彩票| 通江县| 监利县| 西丰县| 景泰县| 黎城县| 贵州省| 来宾市| 通许县| 道真|