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:
- 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;
- 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