hpple——網頁http解析架構

來源:互聯網
上載者:User

標籤:style   http   color   java   使用   io   art   問題   

TFHpple是一個小型的封裝,可以用來解析html,它是對libxml的封裝,文法是xpath。


使用Objective-C解析HTML或者XML,系統內建有兩種方式一個是通過libxml,一個是通過NSXMLParser。


libxml效能較好,且可以結合urlconnection實現邊下載邊解析,在要求快速 、分批響應UI到情況下較為有用,NSXMLParser基本沒什麼優勢,不如使用第三方工具。

Hpple,它是一個輕量級的封裝架構,可以很好的解決這個問題,尤其是它支援HTML的解析,是其他XML類庫所不及的地方,它是用XPath來定位和解析HTML或者XML。

 

使用步驟:

1、 -加入 libxml2 到你的項目中
到Project設定中,選all,找到Search paths下面的
Header Search Paths項目
添加新的 search path “${SDKROOT}/usr/include/libxml2″
注意選擇 Enable recursive option


2、 -加入 libxml2 library 到你的項目
到Target中,選擇Build Phases頁
在Link Binary With Libraries中
從列表中選擇libxml2.dylib


3、 -到這裡下載ZIP包,將下面hpple的原始碼加入到你的項目中:
HTFpple.h
HTFpple.m
HTFppleElement.h
HTFppleElement.m
XPathQuery.h
XPathQuery.m

 

4、 -簡單應用舉例 

  1. #import "TFHpple.h"  
  2.    
  3. NSData *data = [[NSData alloc] initWithContentsOfFile:@"www.baidu.com"];  
  4.    
  5. // Create parser  
  6. xpathParser = [[TFHpple alloc] initWithHTMLData:data];  
  7.    
  8. //Get all the cells of the 2nd row of the 3rd table   
  9. NSArray *elements  = [xpathParser search:@"//title"];  
  10.    
  11. // Access the first cell  
  12. TFHppleElement *element = [elements objectAtIndex:0];  
  13.    
  14. // Get the text within the cell tag  
  15. NSString *content = [element content];   
  16. NSLog(@"%@",content); 
  17.    

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.