Hpple:XPathQuery 库上的一种优美的 Objective-C 包装,用于解析 HTML。
受 why the lucky stiff 的 Hpricot 启发。
Hpple 由 Geoffrey Grosenbach 创建,Topfunky Corporation 和 PeepCode Screencasts。
更多文档和简短视频教程即将推出...
请参阅 Hpple 项目中的 TFHppleHTMLTest.m 以获取示例。
#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