若是給你一個網頁連接, 來抓取指定的內容, 好比豆瓣電影排行榜, 那要怎樣才能作到了?html
其實網頁內容的結構非常相似於XML, 那麼咱們就能夠用解析XML的方式來解析HTML, 不過二者之間的差距仍是很大的, 好了, 廢話很少說, 咱們開始解析HTML。ios
那麼解析XML的庫比較多, 這裏選用libxml來解析, 由於libxml是c語言接口, 找了一個用objective-c包裝接口的庫-hpple, 它的地址是https://github.com/topfunky/hpple, 那麼網頁用的是豆瓣電影排行榜, 地址是http://movie.douban.com/chart。git
接下來新建工程, 工程採用的ARC, 引進libxml2和hpple庫, 新建實體類movie, 完整的項目結構以下: github
movie的實現以下, 這個是實體類, 根據抓取網頁的內容來定這個實體web
movie.h objective-c
@interface Movie : NSObject @property(nonatomic, strong) NSString *name; @property(nonatomic, strong) NSString *imageUrl; @property(nonatomic, strong) NSString *descrition; @property(nonatomic, strong) NSString *movieUrl; @property(nonatomic) NSInteger ratingNumber; @property(nonatomic, strong) NSString *comment; @end
那麼最重要的部分來了, 無論網頁的內容是什麼, 咱們得先須要獲取網頁的內容, 下面就是經過NSURLConnection來獲取整個網頁的內容。數組
- (void)loadHTMLContent { NSString *movieUrl = MOVIE_URL; NSString *urlString = [movieUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; __weak ViewController *weak_self = self; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { if (nil == error) { // NSString *retString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; // NSLog(@"%@", retString); [weak_self parserHTML:data]; } [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; }]; }
這裏只是簡單的獲取網頁內容, 一些HTTP和錯誤處理不在此文討論中, 因此這裏的代碼比較簡單, 在上面的代碼中有個parserHTML:的方法, 就是來解析獲取的網頁內容, 在解析網頁內容以前, 先來扯下xpath。app
假設一個簡單網頁內容以下:atom
<html> <head> <title>Some webpage</title> </head> <body> <p class=」normal」>This is the first paragraph</p> <p class=」special」>This is the second paragraph. <b>This is in bold.</b></p> </body>
好比想獲得title的內容,那麼xpath表達就是/html/head/title。若是想獲得class="special"節點內容,xpath就是/html/body/p[@class='special']。url
因此只要找對了xpath, 就會獲得相應的節點內容, 那麼來看下用hpple解析HTML
- (void)parserHTML:(NSData *)data { if (nil != data) { TFHpple *movieParser = [TFHpple hppleWithHTMLData:data]; NSString *movieXpathQueryString = @"/html/body/div[@id='wrapper']/div[@id='content']/div[@class='grid-16-8 clearfix']/div[@class='article']/div[@class='indent']/table/tr/td/a[@class='nbg']"; NSArray *movieNodes = [movieParser searchWithXPathQuery:movieXpathQueryString]; for (TFHppleElement *element in movieNodes) { Movie *m = [[Movie alloc] init]; m.name = [element objectForKey:@"title"]; m.movieUrl = [element objectForKey:@"href"]; for (TFHppleElement *child in element.children) { if ([child.tagName isEqualToString:@"img"]) { @try { m.imageUrl = [child objectForKey:@"src"]; } @catch (NSException *exception) { } } } [self.movies addObject:m]; } [self.movieTableView reloadData]; } }
代碼中首頁找到相應節點的路徑, 而後searchWithXPathQuery, 獲得一個數組, 遍歷組織數據就可以在table view中展現了。具體效果以下:
好了, 網頁內容被抓取出來了, 具體的實際項目中都要比這個複雜, so, 這只是個引導的示例。
參考:http://www.raywenderlich.com/14172/how-to-parse-html-on-ios#
注: 本文由嘯寒原著,請支持原著!轉載請附上原文連接: http://www.cnblogs.com/xiaohan-wu/p/3203932.html