iOS: 用libxml2 and hpple來做html parser

來源:互聯網
上載者:User

本文是用hpple來做html parser

你還可以選擇用objective-c html parser  https://github.com/zootreeves/Objective-C-HMTL-Parser

或直接用libxml2的NSXMLParser http://www.theappcodeblog.com/2011/07/21/iphone-development-tutorial-parse-html/

(這2個我都沒試過)

1. 建立一個single view application project (with storyboard and arc)

2. 把libxml2 library加到project裡。

     step 1 左邊視窗選定project root node,旁邊會出現一個地區,選擇the node in "TARGETS"

     step 2 select "build phases" tab, expand "Link Binary With Libraries",然後click "+" button

     step 3 search "libxml2", 選定"libxml2.dylib", click "add" button,這時libxml2.dylib會添加到project,出于歸類的目的,建議把它drag and drop to "Frameworks" folder。

     step 4 重複step 1, 然後選定"Build Settings" tab,search "Header Search Paths"並expand it,對於"debug" and "release" node,均通過click "+" button來添加一個value為"${SDK_DIR}"/usr/include/libxml2的item (注意:該值是帶有雙引號的)

     step 5 簡單測試你的project是否添加libxml2成功:在你的view controller .m file裡添加下列代碼,然後看看是否編譯成功,若成功則表示可以使用libxml2 lib

          #import <libxml/HTMLparser.h>

3. 把hpple的源碼添加到project

     step 1 下載hpple from https://github.com/topfunky/hpple

     step 2 在你的project裡create a group (即folder) named "hpple" (這是出于歸類便於管理的目的),然後把下列6個files拖拽進該folder,然後勾上option "copy items into destination group's folder",  選擇option "Create groups for any added folders", 勾上option "Add to Targets", click Finish button

  1. HTFpple.h
  2. HTFpple.m
  3. HTFppleElement.h
  4. HTFppleElement.m
  5. XPathQuery.h
  6. XPathQuery.m

     step 3 這時如果你build your project,就會出現很多"ARC"編譯錯誤。你需要左邊視窗選定project root node,旁邊會出現一個地區,選擇the node in "TARGETS",select "build phases" tab, expand "Compile Sources",double click裡面的hpple的3個.m files,然後添加"-fno-objc-arc"。再次build
project,編譯成功!

4. 最簡單的使用hpple

     在你的view controller .m file裡

     step 1: add 

#import "TFHpple.h"

      step 2: add a method which use apple

- (void) testparser{//    NSString *htmlString=[NSString stringWithContentsOfURL:[NSURL URLWithString: @"https://cap.cityu.edu.hk/default.aspx"] encoding: NSUTF8StringEncoding error:nil];    NSString *htmlString=[NSString stringWithContentsOfURL:[NSURL URLWithString: @"http://www.cwb.gov.tw/eng/index.htm"] encoding: NSUTF8StringEncoding error:nil];        NSData *htmlData=[htmlString dataUsingEncoding:NSUTF8StringEncoding];    TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];       NSArray *elements  = [xpathParser searchWithXPathQuery:@"//title"]; // get the title    //NSArray *elements  = [xpathParser searchWithXPathQuery:@"//td[@class='compact']/a"];     TFHppleElement *element = [elements objectAtIndex:0];    NSString *elementContent = [element content];       NSLog(@"result = %@",elementContent);}

      step 3: 在view controller的“viewDidLoad”裡添加下列代碼

[self testparser];

      step 4: run your app!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.