InObjective-CParsingHTMLThe principle of code implementation is what we will introduce in this article. There is not much content, mainly through code implementation and parsing.HTML. Let's take a look at the detailed implementation.
- # Import "StringChuLi. h"
- /*
- Project role: link network parsing html
- */
-
- @ Implementation StringChuLi
-
- // Access the webpage source code
- -(NSString *) urlString :( NSString *) value {
- NSURL * url = [NSURL URLWithString: value];
- NSData * data = [NSData dataWithContentsOfURL: url];
- // Solve Chinese garbled characters and use GBK
- NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000 );
- NSString * retStr = [[NSString alloc] initWithData: data encoding: enc];
- Return retStr;
- }
- /*
- Purpose: intercept the string from value1 to value2
- Str: string to be processed
- Value1: matching string on the left
- Value2: matching string on the right
- */
- -(NSString *) str :( NSString *) str value1 :( NSString *) value1 value2 :( NSString *) value2 {
- // I: subscript of the matched string in str on the left
- Int I;
- // J: subscript of the matching string in str1 on the right
- Int j;
- // This class can match strings through value1
- Nsange range1 = [str rangeOfString: value1];
- // Determine whether range1 matches a string
- If (range1.length> 0 ){
- // Convert it to NSString
- NSString * result1 = NSStringFromRange (range1 );
- I = [self indexByValue: result1];
- // Cause: add the length of the matching string to obtain the correct subscript.
- Ii = I + [value1 length];
- }
- // Delete the characters before the subscript through the subscript
- NSString * str1 = [str substringFromIndex: I];
- Nsange range2 = [str1 rangeOfString: value2];
- If (range2.length> 0 ){
- NSString * result2 = NSStringFromRange (range2 );
- J = [self indexByValue: result2];
- }
- NSString * str2 = [str1 substringToIndex: j];
- Return str2;
- }
-
- // Filter the subscript of the matching information obtained
- -(Int) indexByValue :( NSString *) str {
- // Use the NSMutableString class to append an object.
- NSMutableString * value = [[NSMutableString alloc] initWithFormat: @ ""];
- NSString * colum2 = @"";
- Int j = 0;
- // Traverse the lower mark Value
- For (int I = 1; I <[str length]; I ++ ){
- NSString * colum1 = [str substringFromIndex: I];
- [Value appendString: colum2];
- Colum2 = [colum1 substringToIndex: 1];
- If ([colum2 isEqualToString: @ ","]) {
- J = [value intValue];
- Break;
- }
- }
- [Value release];
- Return j;
- }
- @ End
Summary: InObjective-CParsingHTMLI hope this article will help you with the introduction of code implementation principles!