关于怎么配置就不说了,具体可以参考:http://lwxshow.com/ios-iphone-development-teaches-you-how-to-use-the-objective-c-parsing-html-lwxshow-com
主要代码如下:
// 1
NSURL *tutorialsUrl = [NSURL URLWithString:@"http://www.olv06.com/lgxw.asp?Clid=22&Chid=1"];
NSData *tutorialsHtmlData = [NSData dataWithContentsOfURL:tutorialsUrl];
// 2
TFHpple *tutorialsParser = [TFHpple hppleWithHTMLData:tutorialsHtmlData];
// 3
NSString *tutorialsXpathQueryString = @"//a";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
for (TFHppleElement *element in tutorialsNodes) {
NSString *url = [element objectForKey:@"href"];
NSLog(@"%@",url);
NSLog(@"%@",[[element firstChild] content]);
}
如果需要的话,可以把解析下来的内容保存到数组里面。
本文介绍如何使用Objective-C中的TFHpple库来解析HTML页面。通过实例代码演示了从指定URL获取HTML内容,利用XPath查询提取所需节点,并打印出链接和文本内容的过程。
1万+

被折叠的 条评论
为什么被折叠?



