您好,登錄后才能下訂單哦!
代碼:https://github.com/zootreeves/Objective-C-HMTL-Parser
?
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | NSString *str = [ NSString stringWithContentsOfURL:[ NSURL URLWithString: @"http://vip.astro.sina.com.cn/astro/view/aries/day/20140808" ] encoding: NSUTF8StringEncoding error: nil ]; NSError *error; //預(yù)先步驟 //1. 在工程中添加libxml2的庫(Build Phase -> Link binary with libraries -> +) //2. 在Header Search Path中添加/usr/include/libxml2 //解析HTML文檔 HTMLParser *parser = [[HTMLParser alloc] initWithString:str error:&error]; if (error) { NSLog ( @"%@" , error); return ; } //獲取HTML的body部分 HTMLNode *node = [parser body]; //根據(jù)HTML節(jié)點的屬性查找節(jié)點 NSArray *tabs = [node findChildrenWithAttribute: @"class" matchingName: @"tab" allowPartial: YES ]; for (HTMLNode *n in tabs) { //根據(jù)節(jié)點名稱查找子節(jié)點 HTMLNode *h5 = [n findChildTag: @"h5" ]; HTMLNode *img = [n findChildTag: @"img" ]; HTMLNode *p = [n findChildTag: @"p" ]; //獲取的屬性值 NSLog ( @"%@" , [img getAttributeNamed: @"src" ]); //獲取節(jié)點內(nèi)容 NSLog ( @"%@: %@" , [h5 contents], [p contents]); } |
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。