Parse HTML code implementation principles in Objective-C

Source: Internet
Author: User

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.

 
 
  1. # Import "StringChuLi. h"
  2. /*
  3. Project role: link network parsing html
  4. */
  5.  
  6. @ Implementation StringChuLi
  7.  
  8. // Access the webpage source code
  9. -(NSString *) urlString :( NSString *) value {
  10. NSURL * url = [NSURL URLWithString: value];
  11. NSData * data = [NSData dataWithContentsOfURL: url];
  12. // Solve Chinese garbled characters and use GBK
  13. NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000 );
  14. NSString * retStr = [[NSString alloc] initWithData: data encoding: enc];
  15. Return retStr;
  16. }
  17. /*
  18. Purpose: intercept the string from value1 to value2
  19. Str: string to be processed
  20. Value1: matching string on the left
  21. Value2: matching string on the right
  22. */
  23. -(NSString *) str :( NSString *) str value1 :( NSString *) value1 value2 :( NSString *) value2 {
  24. // I: subscript of the matched string in str on the left
  25. Int I;
  26. // J: subscript of the matching string in str1 on the right
  27. Int j;
  28. // This class can match strings through value1
  29. Nsange range1 = [str rangeOfString: value1];
  30. // Determine whether range1 matches a string
  31. If (range1.length> 0 ){
  32. // Convert it to NSString
  33. NSString * result1 = NSStringFromRange (range1 );
  34. I = [self indexByValue: result1];
  35. // Cause: add the length of the matching string to obtain the correct subscript.
  36. Ii = I + [value1 length];
  37. }
  38. // Delete the characters before the subscript through the subscript
  39. NSString * str1 = [str substringFromIndex: I];
  40. Nsange range2 = [str1 rangeOfString: value2];
  41. If (range2.length> 0 ){
  42. NSString * result2 = NSStringFromRange (range2 );
  43. J = [self indexByValue: result2];
  44. }
  45. NSString * str2 = [str1 substringToIndex: j];
  46. Return str2;
  47. }
  48.  
  49. // Filter the subscript of the matching information obtained
  50. -(Int) indexByValue :( NSString *) str {
  51. // Use the NSMutableString class to append an object.
  52. NSMutableString * value = [[NSMutableString alloc] initWithFormat: @ ""];
  53. NSString * colum2 = @"";
  54. Int j = 0;
  55. // Traverse the lower mark Value
  56. For (int I = 1; I <[str length]; I ++ ){
  57. NSString * colum1 = [str substringFromIndex: I];
  58. [Value appendString: colum2];
  59. Colum2 = [colum1 substringToIndex: 1];
  60. If ([colum2 isEqualToString: @ ","]) {
  61. J = [value intValue];
  62. Break;
  63. }
  64. }
  65. [Value release];
  66. Return j;
  67. }
  68. @ End

Summary: InObjective-CParsingHTMLI hope this article will help you with the introduction of code implementation principles!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.