开发者

Extract table from web output

开发者 https://www.devze.com 2023-03-07 16:14 出处:网络
I am able to pull a webpage into my code 开发者_StackOverflowas a long string. Now I want to remove everything before a <table> tag, and after the <table> tag - so I only retain the table

I am able to pull a webpage into my code 开发者_StackOverflowas a long string. Now I want to remove everything before a <table> tag, and after the <table> tag - so I only retain the table itself.

I am thinking about either using NSScanner, or regexp. Which do you think is more efficient/easier to code manage? The page may also (in the future) contain two tables, and I would want to extract each one (one after the other) for display in my view.

Any ideas?

And thanks in advance!

Kolya


hi you can use this code

 - (NSMutableArray *)stringFilter:(NSString *)targetString {

        NSScanner *theScanner;
        NSString *text = nil;

        theScanner = [NSScanner scannerWithString: targetString];

        while ([theScanner isAtEnd] == NO) {

            [theScanner scanUpToString:@"<table>" intoString:NULL] ; 

            [theScanner scanUpToString:@"</table>" intoString:&text] ;

           [tempArray addObject:text];

        } 

        return tempArray;

    }

the temp array will contain all the strings in table tag got help from this site


So - I used NSScanner.

// calling a service to extract the webpage as a string..
NSString *thewebpage = [NSString stringWithFormat:@"%@", [self getthewebstring]];
NSString *tmpwebpage = @"";

// Assign 'theScanner' to class NSScanner
NSScanner *theScanner = [NSScanner scannerWithString:tmpwebpage];

// find start of TABLE
[theScanner scanUpToString:@"<h2>Table Header</h2>" intoString:nil];
[theScanner scanUpToString:@"</table>" intoString:&tmpwebpage];
NSString *myTableOnlyData = [tmpwebpage stringByAppendingString:@"</table>"];

This worked very well for me.
Kolya

0

精彩评论

暂无评论...
验证码 换一张
取 消