我有来自webservice的字符串,我需要从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
如何从Time
字符串中获取location
,details
和HTML
的价值
由于
答案 0 :(得分:0)
您可以使用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];
答案 1 :(得分:0)
您可以使用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