Web page parsing in iOS non-UTF-8 format

Source: Internet
Author: User

There are a lot of methods on the Internet for iOS XML parsing, there are a lot of page parsing about non-UTF-8 format, I tried to read several, but did not succeed. Today inadvertently but it was done, so I want to share with you. In fact very simple, the following is how to get a non-UTF-8 format of the page to parse the code content, this is a more critical step, the rest of the parsing process online a lot, here is not to repeat, if there is a need to talk, and then contact me.

1, take Baidu (http://www.baidu.com) as an example, its Web page format is gb2312, create a new project, in the-(void) viewdidload input content:

1NSURL *url = [Nsurl urlwithstring:@ "http://www.baidu.com"];
2
3NSStringEncoding encode = cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000);
4NSData *data = [NSData datawithcontentsofurl:url];//get Hex data
5NSLog (@ "data:%@", data);
6CSS3 Animation
7NSString *str = [[NSString alloc] Initwithdata:data Encoding:encode];
8NSLog (@ "str:%@", str);
Part of the first NSLog output:

Part of the second NSLog output:

This is the GB2312 format of the Web page, through the "right click-View element" can be obtained, but also we want to parse the content.

In fact, the above code can also be written directly:

1NSURL *url = [Nsurl urlwithstring:@ "http://www.baidu.com"];

2NSStringEncoding encode = cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000);
3 http://www.huiyi8.com/css3/?
4NSString *str = [[NSString alloc] Initwithcontentsofurl:url encoding:encode Error:nil];
5NSLog (@ "htmlencoding:%@", str);

The resulting content is the same as the second output data in the first case.

2, consider a moment, I put UTF-8 format page parsing the first step also write down, in order to compare. Take Sina Weibo (http://www.weibo.com) as an example, its Web page format is UTF-8, in the-(void) viewdidload input content:

1NSURL *url2 = [Nsurl urlwithstring:@ "http://www.weibo.com"];

2NSString *STR2 = [NSString stringwithcontentsofurl:url2 encoding:nsutf8stringencoding Error:nil];
3NSLog (@ "str2:%@", str2);

Partial content of the output:

?

This is what we want to parse, the rest of the parsing needs to add the corresponding library, file, modify build settings settings, and so on, these online have, do not say.

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.