前言:
專案中遇見一個需求遍歷網頁中所有的<img>標籤並且去處圖片的url
第一步:編寫獲取<img >標籤的正規表示式,程式碼如下:
-(NSArray*)getImgTags:(NSString *)htmlText { if (htmlText == nil) { return nil; } NSError *error; NSString *regulaStr = @"<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>"; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr options:NSRegularExpressionCaseInsensitive error:&error]; NSArray *arrayOfAllMatches = [regex matchesInString:imageText options:0 range:NSMakeRange(0, [htmlText length])]; return arrayOfAllMatches; }
第二步:根據html標籤元素獲取圖片url
-(NSString *)getImageUrl:(NSString *)imgTagStr{ // 提取url NSData * data = [imgTagStr dataUsingEncoding:NSUTF8StringEncoding]; TFHpple *doc = [[TFHpple alloc] initWithXMLData:data]; TFHppleElement *e = [doc peekAtSearchWithXPathQuery:@"//img"]; return [self encodeStringFromString:[e objectForKey:@"src"]]; }