Simple libxml2 HTML parsing example, using Objective-c, Xcode, and HTMLparser.h

后端 未结 2 579
心在旅途
心在旅途 2020-12-01 08:31

Please can somebody show me a simple example of parsing some HTML using libxml.

#import 

NSString *html = @\"
    \"
2条回答
  •  情书的邮戳
    2020-12-01 09:30

    I used Ben Reeves' HTML Parser to achieve what I wanted:

    NSError *error = nil;
    NSString *html = 
        @"
      " "
    • " "
    • " "
    " "Hello World 1" "Hello World 2"; HTMLParser *parser = [[HTMLParser alloc] initWithString:html error:&error]; if (error) { NSLog(@"Error: %@", error); return; } HTMLNode *bodyNode = [parser body]; NSArray *inputNodes = [bodyNode findChildTags:@"input"]; for (HTMLNode *inputNode in inputNodes) { if ([[inputNode getAttributeNamed:@"name"] isEqualToString:@"input2"]) { NSLog(@"%@", [inputNode getAttributeNamed:@"value"]); //Answer to first question } } NSArray *spanNodes = [bodyNode findChildTags:@"span"]; for (HTMLNode *spanNode in spanNodes) { if ([[spanNode getAttributeNamed:@"class"] isEqualToString:@"spantext"]) { NSLog(@"%@", [spanNode allContents]); //Answer to second question } } [parser release];

提交回复
热议问题