在Objective-C中从HTML解析图片 [英] Parse picture from HTML in Objective-C

查看:147
本文介绍了在Objective-C中从HTML解析图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < div class =image>我试图从这个html数据中检索图像。 
< a href =http://www.website.com/en/105/News/10217/>
width =105height =110alt =kollsge(photo:作者)/>
< / a>
< / div>

这是我的代码:

  HTMLNode * bodyNode = [parser body]; 

NSArray * imageNodes = [bodyNode findChildTags:@div];
$ b $ for(HTMLNode * imageNode in imageNodes){
if([[imageNode getAttributeNamed:@class] isEqualToString:@image]){
NSLog(@ %@,[imageNode getAttributeNamed:@img src]);


帮助将不胜感激。



我解决了这个问题:

  for(HTMLNode * imageNode in imageNodes){ 
if([[imageNode getAttributeNamed:@class] isEqualToString:@image]){
HTMLNode * aNode = [imageNode firstChild];
HTMLNode * imgNode = [aNode nextSibling];
HTMLNode * imNode = [imgNode firstChild];
NSLog(@%@,[imNode getAttributeNamed:@src]);



$ div $解析方案

你没有正确地通过树。您正尝试在 div 上查找名为 img src 的属性。这看起来像这样:

 < div class =imageimg src =whatever> 

首先,这不是有效的HTML,但更重要的问题是您想要查看在儿童。你正在寻找的东西嵌套在div中,而不是属性。由于您的div只有一个孩子,所以快速浏览一下您在评论中提供的项目后,我会相信以下内容会有效:

  HTMLNode * bodyNode = [parser body]; 

NSArray * imageNodes = [bodyNode findChildTags:@div]; $ {
$ if((imageNode getAttributeNamed:@class] isEqualToString:@image]){
HTMLNode * aNode = [imageNode firstChild];
HTMLNode * imgNode = [aNode nextSibling];
NSLog(@%@,[imgNode getAttributeNamed:@src]);
}
}


I am trying to retrieve the image from this html data:

<div class="image">
 <a href="http://www.website.com/en/105/News/10217/">
   <img src="/images/cache/105x110/crop/images%7Ccms-image-000005554.gif" 
        width="105" height="110" alt="kollsge (photo: author)" />
 </a>
</div>

This is my code:

HTMLNode *bodyNode = [parser body];

NSArray *imageNodes = [bodyNode findChildTags:@"div"];

for (HTMLNode *imageNode in imageNodes) {
    if ([[imageNode getAttributeNamed:@"class"] isEqualToString:@"image"]) {
        NSLog(@"%@", [imageNode getAttributeNamed:@"img src"]);
    } 
}

Help would be much appreciated.

I solved it by this code:

 for (HTMLNode *imageNode in imageNodes) {
        if ([[imageNode getAttributeNamed:@"class"] isEqualToString:@"image"]) {
            HTMLNode *aNode = [imageNode firstChild];
            HTMLNode *imgNode = [aNode nextSibling];
            HTMLNode *imNode = [imgNode firstChild];
            NSLog(@"%@", [imNode getAttributeNamed:@"src"]);
        } 
    }

解决方案

You are not going through the tree correctly. You are attempting to find an attribute named img src on your div. That would look like this:

<div class="image" img src="whatever">

For one thing, that's not valid HTML, but the more important issue is that you want to be looking at the children. The thing you are looking for is nested inside the div, not an attribute. Since your div only has one child, a quick look at the project you provided in the comments leads me to believe that the following will work:

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];
        NSLog(@"%@", [imgNode getAttributeNamed:@"src"]);
    } 
}

这篇关于在Objective-C中从HTML解析图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆