iOS development--Tfhpple crawl GB-2312 encoded HTML page, page return encoding error

Source: Internet
Author: User

Today with Tfhpple parsing HTML, found that the old error encoding Error:input conversion failed due to input error, bytes I/o error:encoder error A look will know is a compilation Code of the problem ah, but I clearly have nsdata (GB-2312)----------->nsstring-------> (NSUTF-8) nsdata These variables should be not wrong, and found that the original is, Do not perform this step will be one line of <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "> converted to UTF-8 form

The idea of parsing non-utf-8 pages:

    1. The GB2312 (assuming this) of the NSDate returned by the network stream is converted into the nsdata of UTF-8 encoding which tfhpple can parse correctly;
    2. The <meta of one line http-equiv= "Content-type" content= "text/html; charset=gb2312 "> converted to UTF-8 form

The specific code is as follows:

1 //1. Get the data2Nsurl *postdetailurl = [Nsurl urlwithstring:[nsstring stringWithFormat:@"%@%@", BASEURL, Self.topicurl]];3NSData *postdetailhtmldata =[NSData Datawithcontentsofurl:postdetailurl];4     5     //2. Transcode to Utf8data: First turn to gb2312, replace Meta, and turn to UTF86Nsstringencoding gbkencoding =cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000);7NSString *posthtmlstr =[[NSString alloc] Initwithdata:postdetailhtmldata encoding:gbkencoding];8 9NSString *uft8htmlstr = [Posthtmlstr stringbyreplacingoccurrencesofstring:@"<meta http-equiv=\"Content-type\"content=\ "text/html; charset=gb2312\" >"Withstring:@"<meta http-equiv=\"Content-type\"content=\ "text/html; charset=utf-8\" >"];TenNSData *utf8htmldata = [Uft8htmlstr datausingencoding:nsutf8stringencoding];

http://blog.csdn.net/toolazytoname/article/details/10051681

iOS development--Tfhpple crawl GB-2312 encoded HTML page, page return encoding error

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.