I am trying to get href value from a html page
NSURL *url = [[searchBar.text stringByReplacingOccurrencesofString:@" " withString:@"+"]];
NSString *html = [NSString stringWithContentOfURL:url encoding:NSASCIIStringEncoding error:&error
NSLog(@"Html = %@",html);
So far, up to this point of the code, it is working perfectly, in terms of getting the html from youtube and displaying the html for the first page of the search result.
The next thing i tried to do is to remove some of the unwanted HTML tages that i do not need like ,,,, etc. All i want left would be the and the tags and their values or data with them.
So i have tried using NSScanner and Regex.But i can’t seem to get the desired result. The code that I am now trying is
NSScanner *scanner = [NSScanner scannerWithString:self];
[scanner setCharactersToBeSkipped:nil];
NSMutableSet *tags = [[NSMutableSet alloc] init];
NSString *tag;
do {
tag = nil;
[scanner scanUpToString:@"<" intoString:NULL];
[scanner scanUpToString:@">" intoString:&tag];
if (tag) {
NSString *t = [[NSString alloc] initWithFormat:@"%@>", tag];
[tags addObject:t];
}
} while (![scanner isAtEnd]);
NSMutableString *result = [[NSMutableString alloc] initWithString:self];
NSString *finalString;
NSString *replacement;
for (NSString *t in tags) {
replacement = @" ";
if ([t isEqualToString:@"<a>"] ||
[t isEqualToString:@"</a>"] ||
[t isEqualToString:@"<span>"] ||
[t isEqualToString:@"</span>"] ||
[t isEqualToString:@"<strong>"] ||
[t isEqualToString:@"</strong>"] ||
[t isEqualToString:@"<em>"] ||
[t isEqualToString:@"</em>"]||
[t isEqualToString:@"<script>"]||
[t isEqualToString:@"</script>"])
{
replacement = @"";
}
[result replaceOccurrencesOfString:t withString:replacement options:NSLiteralSearch MakeRange(0, result.length)];
}
finalString = [result stringByRemovingNewLinesAndWhitespace];
Convert formatted HTML text string to NSString parts
This is where i refered from. But still i can’t get it to work the way i need it to.I have been stuck for many days on the same problem, please help me.Any advice on how i should go about doing it would also help greatly.Thanks in advance.
Your best bet will be to modify the NSString+HTML category from your linked post. For example if you don’t want to remove the
<a>tags, you could modify the code to something like this:Insert this to line 67 of the file linked above:
And remove the
<a>conditions from the if following that line.Feel free to read, analyze and play with that code until you get what you need: all in all the best thing in open source world is that you can study everything what you don’t know from the already written code.