This the part of HTML from which I am trying to retrieve an image.
div class="image">
<a href="http://www.web.com/EN/105/News/10228/"><img src="/images/cache/360x295/crop/images%7Ccms-image-000007796.jpg" width="360" height="295" alt=" (photo: )" /></a>
</div>
I can separately get “href” and “img src” values but none of them are the link that would let me reach the image. The valid link would be http://www.web.com plus img src value. I thought to append the string: http://www.web.com to img src but couldn’t figure out how to do it and not sure that this is the right approach. So, what I am trying to get is http://www.web.com/images/cache/360x295/crop/images%7Ccms-image-000007796.jpg
This is my code through which I am parsing the HTML data:
-(void) fethchData
{
NSError *error = nil;
NSURL *url=[[NSURL alloc] initWithString:@"http://www.web.com/"];
NSString *strin=[[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
HTMLParser *parser = [[HTMLParser alloc] initWithString:strin error:&error];
if (error) {
NSLog(@"Error: %@", error);
return;
}
HTMLNode *bodyNode = [parser body];
NSArray *imageNodes = [bodyNode findChildTags:@"div"];
for (HTMLNode *imageNode in imageNodes) {
if ([[imageNode getAttributeNamed:@"class"] isEqualToString:@"image"]) {
HTMLNode *aNode = [imageNode firstChild];
HTMLNode *imgNode = [aNode nextSibling];
HTMLNode *imNode = [imgNode firstChild];
NSURL* imageURL = [NSURL URLWithString:[imNode getAttributeNamed:@"src"] relativeToURL:url];
NSLog(@"%@", imageURL);
}
}
}
This is the output: /images/cache/360x295/crop/images%7Ccms-image-000007653.jpg -- http://www.web.ge/
I need to see http://www.web.ge/ first and then the rest.
Use:
Update: if you feel you must have an absolute URL object, you could follow the above with: