iphone - 如何解析 iPhone 应用程序中的 HTML 子标签?

标签 iphone html ios tags html-parser

我有一个包含大量图像和实时内容的 HTML 网页。我需要解析网页中的数据(HTML) 并显示在iPhone 应用程序中。我正在使用以下代码来解析 HTML 内容。但是我不知道如何解析标签中的子标签?

{    
    NSURL *url = [NSURL URLWithString:@"http://www.samplewebpage.com/vd/t/1/830.html"]; 

    NSData *data = [[NSData alloc] initWithContentsOfURL:url];
    NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"Response : %@", responseString);

    NSMutableArray *imageURLArray = [[NSMutableArray alloc] init];
    NSMutableArray *divClassArray = [[NSMutableArray alloc] init];
    //NSString *regexStr = @"<A HREF=\"([^>]*)\">";

    // For image : 1. img src=\"([^>]*)\"  2. <img src=\"([^>]*)\">
    // For getting Class div class
    NSString *regularExpressString = @"div class=\"([^>]*)\"";
    NSError *error;
    NSInteger i =0;
    while (i<[responseString length]) 
    {
        NSRegularExpression *testRegularExpress = [NSRegularExpression regularExpressionWithPattern:regularExpressString options:NSRegularExpressionCaseInsensitive error:&error];

        if( testRegularExpress == nil ) 
        {
            NSLog( @"Error making regex: %@", error );
        }

        NSTextCheckingResult *textCheckingResult = [testRegularExpress firstMatchInString:responseString options:0 range:NSMakeRange(i, [responseString length]-i)];
        NSRange range = [textCheckingResult rangeAtIndex:1];
        if (range.location == 0) 
        { 
            break;
        }
        NSString *classNameString = [responseString substringWithRange:range];
        NSLog(@"Div Class Name : %@", classNameString);

        [divClassArray addObject:classNameString];

        i= range.location;
        //NSLog(@"Range.location : %i",range.location);
        i=i+range.length;
    }

    NSLog(@"divClass Array : %@, Count : %d", divClassArray, [divClassArray count]);
}

响应:

<div class="phoneModelItems" style="width:30%;margin-right:4px;"><a href="javascript:noxLatestChart.navToLatestChart('latest');" style="font-weight:italic;">Nokia Model</a></div>

我想从 phoneModelItems 类中获取文本 Nokia Model。你能告诉我如何检索文本“Nokia Model”吗?提前致谢。

最佳答案

这是我针对您的问题的正则表达式:

<div\sclass=\"phoneModelItems\".*?><a\shref.*?>(.*?)<\/a><\/div>

你可以在 Rubular 上测试它

关于iphone - 如何解析 iPhone 应用程序中的 HTML 子标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10996903/

相关文章:

ios - 基于关系获取数据的核心数据谓词给出错误“无法解析格式字符串”

ios - 如何根据不同的形状拼贴图像

iphone - 应用程序挂起和崩溃(4.3.5 的问题)

iphone - UIScrollView 将触摸发送到 subview

javascript - 如何将while循环添加到随机发生器

ios - 来自 RemoteIO : resulting . caf 的录音音高偏移较慢 + 失真

ios - swift 位置管理器

javascript - 如何使用js使幻灯片图像可点击以在同一页面上打开图像

javascript - 我怎样才能实现这个水平搜索结果栏?

objective-c - Dealloc 未被调用