问题
I have a very simple html page to parse. The html page will remain simple always. as simple as this
<html>
<head><title>title</title></head>
<body>some data here</body>
</html>
I have fetched the html content of such an html page and have it in an NSString. I want to get what ever data is there in the body of the html page.
Please tell me how can this be done and let me know if there are more than one possible ways. I would prefer doing it using basic obj-c if it is possible.
Thanks
回答1:
If you control the content of the html page, you could simply use NSScanner to look for the tags you want. If you're looking for something that can handle more a complex set of tags, look into use NSXMLParser.
回答2:
Use NSScanner:
NSString *parsed;
NSScanner *scanner = [NSScanner scannerWithString: sourcecode];
[nscanner scanUpToString:@"<body>" intoString:NULL];
[nscanner scanUpToString:@"</body>" intoString: &parsed];
NSMutableString *finalparsed = [[NSMutableString alloc] initWithString: parsed];
[finalparsed deleteCharactersInRange:NSMakeRange(0, 6)]; //To get rid of <body>
finalparsed will contain only "some data here", or whatever is in between the body tags.
来源:https://stackoverflow.com/questions/2739670/parsing-simple-html-for-iphone