Parsing Html url in iphone

2020-06-06 06:55发布

How do I parse an html file?

I need the data in between span tag.

<div id=currency_converter_result>1 AED = <span class=bld>0.4765 ANG</span>

标签: html ios parsing
3条回答
该账号已被封号
2楼-- · 2020-06-06 07:41

Since XHTML is XML, you can consider using NSXMLParser:

@interface HTMLParser: NSObject <NSXMLParserDelegate> // or whichever superclass you have
{
    // own declarations
    NSMutableString *str;
    NSXMLParser *parser;
}

// somewhere in a method of self, for example, init, or something named -(void) parseHtml
- (void) parseHtml
{
    parser = [[NSXMLParser alloc] initWithData:[@"<div id=currency_converter_result>1 AED = <span class=bld>0.4765 ANG</span>" dataUsingEncoding:NSUTF8StringEncoding]];
    // of course you can substitute any string you want here, for example, the result of [NSString stringWithContentsOfFile:@"inex.html"] or whatever you need.
    parser.delegate = self;
    [parser parse];
    [parser release];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"span"] && [[attributeDict objectForKey:@"class"] isEqualToString:@"bld"])
    {
        str = [NSMutableString string];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [str appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"span"])
    {
        // now str contains the value you want!
        DoSomethingWith(str);
    }
}

Hope this helps.

查看更多
Summer. ? 凉城
3楼-- · 2020-06-06 07:42

Check out this: https://github.com/zootreeves/Objective-C-HMTL-Parser

Basic Usage :

NSError *error = nil;
NSString *htmlString = 
    @"<div id=currency_converter_result>1 AED = <span class=bld>0.4765 ANG</span>";
HTMLParser *p = [[HTMLParser alloc] initWithString:htmlString error:&error];

if (error) {
    NSLog(@"Error: %@", error);
    return;
}

HTMLNode *bodyNode = [p body];

NSArray *spanNodes = [bodyNode findChildTags:@"span"];

for (HTMLNode *spanNode in spanNodes) {
    if ([[spanNode getAttributeNamed:@"class"] isEqualToString:@"bld"]) {
        NSLog(@"%@", [spanNode rawContents]); //Answer to second question
    }
}
查看更多
何必那么认真
4楼-- · 2020-06-06 07:47

If you need only tag 'span' you can use NSRegularExpression such as this one

NSString *html = @"<div id=currency_converter_result>1 AED = <span class=bld>0.4765 ANG</span>";
NSRegularExpression *regex = [NSRegularExpression
                               regularExpressionWithPattern:@"<span[^>]*>(.+?)</span>"
                               options:NSRegularExpressionCaseInsensitive
                               error:nil];
NSTextCheckingResult *textCheckingResult = [regex firstMatchInString:html options:0 range:NSMakeRange(0, html.length)];
NSLog(@"found: '%@'", [html substringWithRange:[textCheckingResult rangeAtIndex:1]]);

or, I prefer to create a NSDictionary from xml or html data and work with it. You can do it with XML-to-NSDictionary library.

查看更多
登录 后发表回答