+ (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ù)信息
新聞熱點
疑難解答