前言:
项目中遇见一个需求遍历网页中所有的<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: range:NSMakeRange(, [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"]];
}