【问题标题】:How can I get value from HTML tag in IOS?如何从 IOS 中的 HTML 标签中获取价值?
【发布时间】:2016-06-17 15:13:22
【问题描述】:

我有来自网络服务的字符串,我需要从 HTML 标记中获取值。

<b>Time:</b> Sunday, 17 July 2016 at 18:00<br /><b>Details:</b> Plug in to a regular feast of glorious gospel revelation! / Visit www.TheNewMystics.TV to become a premium member. Each month, John Crowder interacts live with viewers, answering questions, teaching and releasing the glory via live stream. / Members can send in questions live, enjoy each month's broadcast and have access to all archive teachings. / / Starts at 6 p.m. Pacific/ 9 p.m. Eastern<br /><b>Location:</b> www.thenewmystics.tv

如何从HTML 字符串中获取Time、location 和details 的值

谢谢

【问题讨论】:

标签: html ios objective-c


【解决方案1】:

您可以使用substringWithRange。在两个范围之间传递 HTML 标记,您将得到它们之间的字符串。

NSRange r1 = [s rangeOfString:@"<b>Time:</b>"];
NSRange r2 = [s rangeOfString:@"<br />"];
NSRange rSub = NSMakeRange(r1.location + r1.length, r2.location - r1.location - r1.length);
NSString *sub = [s substringWithRange:rSub];

【讨论】:

    【解决方案2】:

    你可以使用hpple!

    如:

    #import "TFHpple.h"
    
    NSData  * data      = [NSData dataWithContentsOfFile:@"index.html"];
    
    TFHpple * doc       = [[TFHpple alloc] initWithHTMLData:data];
    NSArray * elements  = [doc search:@"//a[@class='sponsor']"];
    
    TFHppleElement * element = [elements objectAtIndex:0];
    [e text];                       // The text inside the HTML element (the content of the first text node)
    [e tagName];                    // "a"
    [e attributes];                 // NSDictionary of href, class, id, etc.
    [e objectForKey:@"href"];       // Easy access to single attribute
    [e firstChildWithTagName:@"b"]; // The first "b" child node
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-12
      • 1970-01-01
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      • 2017-04-02
      相关资源
      最近更新 更多